Convert alphabet letters to number in Python

后端 未结 16 1740
慢半拍i
慢半拍i 2020-11-28 05:09

How can the following be finished?

characters = [\'a\'\'b\'\'c\'\'d\'\'e\'\'f\'\'g\'\'h\'\'i\'\'j\'\'k\'\'l\'\'m\'\'n\'\'o\'\'p\'\'q\'\'r\'\'t\'\'u\'\'v\'\'w         


        
相关标签:
16条回答
  • 2020-11-28 05:32

    Not to be too basic, but this:

    >>> char1 = ['a''b''c''d''e''f''g''h''i''j''k''l'
                 'm''n''o''p''q''r''s''t''u''v''w''x''y''z']
    

    is very different than this:

    >>> char2 = ['a','b','c','d','e','f','g','h','i','j','k','l',
                   'm','n','o','p','q','r','s','t','u','v','w','x','y','z']
    

    The first, without commas and what you have in your question, is a one element list with a 26 element string. The second is a 26 element list each a single character in length.

    If you print each:

    >>> print char1, len(char1), len(char1[0])
    ['abcdefghijklmnopqrstuvwxyz'] 1 26
    >>> print char2, len(char2), len(char2[0])
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
    'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 26 1
    

    It becomes apparent that it takes an additional step to turn the individual characters of char1 into an iterable.

    If you have the sequence of characters 'a' through 'z' and/or 'A' through 'Z', you can easily return the number of the character with list comprehension:

    >>> [ord(x)%32 for x in char2]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    

    For the type of data structure you have, you need to access the string first:

    >>> [ord(x)%32 for x in char1[0]]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    

    So if your code listing is the same as in your question, that may be your issue.

    A reasonable alternative is: [ord(x.lower())-96 for x in char1[0]]

    You can see that your characters=['a''b''c'...], without the commas, is just the same as typing all the characters in a string in a list like this ['abc...'].

    So now try:

     >>> import string
     >>> [ord(x.lower())-96 for x in string.letters]
     [1,2,...26, 1,2,3...26]      # my ellipses 
     >>> char3=[string.letters]   # one string as element[0]
     >>> [ord(x)%32 for x in char3[0]]
     >>> [ord(x)%32 for x in [string.letters][0]]
    
    0 讨论(0)
  • 2020-11-28 05:33

    Something like this

    [str(ord(c)&31) for c in text]
    
    0 讨论(0)
  • 2020-11-28 05:33
    >>> [str(ord(string.lower(c)) - ord('a') + 1) for c in string.letters]
    ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17',
    '18', '19', '20', '21', '22', '23', '24', '25', '26', '1', '2', '3', '4', '5', '6', '7', '8',
    '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24',
     '25', '26']
    
    0 讨论(0)
  • 2020-11-28 05:35

    What about something like this:

    print [ord(char) - 96 for char in raw_input('Write Text: ').lower()]
    

    ord
    list comprehension
    ASCII character codes

    EDIT
    Since you asked me to explain I will... though it has been explained pretty well in the comments already by [?].

    Let's do this in more that one line to start.

    input = raw_input('Write Text: ')
    input = input.lower()
    output = []
    for character in input:
        number = ord(character) - 96
        output.append(number)
    print output
    

    This does the same thing, but is more readable. Make sure you can understand what is going on here before you try to understand my first answer. Everything here is pretty standard, simple Python. The one thing to note is the ord function. ord stand for ordinal, and pretty much every high level language will have this type of function available. It gives you a mapping to the numerical representation of any character. The inverse function of ord is called chr.

    chr(ord('x')) == 'x' # for any character, not just x.
    

    If you test for yourself, the ordinal of a is 97 (the third link I posted above will show the complete ASCII character set.) Each lower case letter is in the range 97-122 (26 characters.) So, if you just subtract 96 from the ordinal of any lower case letter, you will get its position in the alphabet assuming you take 'a' == 1. So, ordinal of 'b' == 98, 'c' == 99, etc. When you subtract 96, 'b' == 2, 'c' == 3, etc.

    The rest of the initial solution I posted is just some Python trickery you can learn called list comprehension. But, I wouldn't focus on that as much as I would focus on learning to solve the problem in any language, where ord is your friend. I hope this helps.

    0 讨论(0)
  • 2020-11-28 05:35

    This is a function I used to use for this purpose. Works for both uppercase and lowercase.

    def convert_char(old):
        if len(old) != 1:
            return 0
        new = ord(old)
        if 65 <= new <= 90:
            # Upper case letter
            return new - 64
        elif 97 <= new <= 122:
            # Lower case letter
            return new - 96
        # Unrecognized character
        return 0
    
    0 讨论(0)
  • 2020-11-28 05:40

    You can use chr() and ord() to convert betweeen letters and int numbers.

    Here is a simple example.

    >>> chr(97)
    'a'
    >>> ord('a')
    97
    
    0 讨论(0)
提交回复
热议问题