【问题标题】:Using a dict to translate numbers to letters in python使用字典将数字转换为python中的字母
【发布时间】:2018-02-21 21:05:39
【问题描述】:

我有一个字符串“alphabet”,其中包含字母表中的所有字母以及与这些字母 (0-25) 对应的整数列表。

例如:

num_list = [5,3,1] would translate into letter_list = ['f','d','b']

我目前可以翻译:

letter_list = [alphabet[a] for a in num_list]

但是,我想为同样的事情使用 dict,从具有 'number' 值的 dict 中检索 'letter' 键。

alpha_dict = {'a':0,'b':1,'c':2}... etc

如何更改我的声明来做到这一点?

【问题讨论】:

  • 你想给字典提供数值并取回字母键吗?将您的密钥设置为您提供的数字和字母的值会容易得多,这样alpha_dict[0] 就会为您提供'a' 等。
  • 请注意,您可以使用index 在原始列表中查找项目的索引。 num_list = [alphabet.index(letter) for letter in letter_list]

标签: python list dictionary


【解决方案1】:

只需遍历您的 alphabet 字符串,并使用字典理解来创建您的字典

# Use a dictionary comprehension to create your dictionary
alpha_dict = {letter:idx for idx, letter in enumerate(alphabet)}

然后您可以使用alpha_dict[letter] 检索任何字母的对应数字,将letter 更改为您想要的任何字母。

然后,如果您想要一个与您的num_list 对应的字母列表,您可以这样做:

[letter for letter, num in alpha_dict.items() if num in num_list]

本质上说:对于我字典中的每个键值对,如果值(即数字)在num_list中,则将键(即字母)放入列表中

这将为您提供的num_list 返回['b', 'd', 'f']

【讨论】:

  • 是的,这是一个字典生成器表达式。
  • 您不需要将alphabet 转换为列表。字符串是可迭代的,所以你可以直接将它传递给enumerate
  • @ekhumoro 酷!感谢您指出这一点,我已经编辑了我的答案以考虑到这一点。
【解决方案2】:

您也可以反转alpha_dict 来执行此操作:

>>> import string
>>> alpha_dict = dict(enumerate(string.ascii_lowercase))
>>> alpha_dict
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}
>>> numbers = [5, 3, 1]
>>> [alpha_dict[x] for x in numbers]
['f', 'd', 'b']

这样,您的字典将数字映射到字母,而不是字母映射到数字。

【讨论】:

    【解决方案3】:

    您可以像这样构建字母/数字的字典:

    import string
    
    alpha_dict = {
        i: string.ascii_lowercase[i]
            for i in list(range(0, len(string.ascii_lowercase)))
    }
    
    num_list = [5,3,1]
    letter_list = [alpha_dict.get(a) for a in num_list]
    

    但是,像这样使用列表会更容易:

    import string
    
    num_list = [5,3,1]
    letter_list = [string.ascii_lowercase[a] for a in num_list]
    

    【讨论】:

      【解决方案4】:

      我认为 - 就像其他答案一样 - 应该使用列表(使用方便的索引调用)但不应该使用字典,句号。所以……

          from string import ascii_lowercase # ONLY import ascii_lowercase
          numList = [numbers, in, your, list] # like you said in your question
          translation = "".join ([ascii_lowercase[num] for num in numList])
      

      【讨论】:

        【解决方案5】:
        letters = [chr(i) for i in range(97, 123)]
        alph_index = dict(zip(letters, range(26)))
            
        print(">> letters=", letters)
        print(">> alph_index=", alph_index)
        
        • ord('a') = 97
        • chr(97) = 'a'
        • 97是a,65是A,122是z
        Output:
        >> letters= ['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']
        
        >> alph_index= {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
        

        【讨论】:

          猜你喜欢
          • 2015-12-17
          • 2017-07-21
          • 2011-05-30
          • 1970-01-01
          • 2014-06-05
          • 1970-01-01
          • 1970-01-01
          • 2013-02-19
          • 2016-10-16
          相关资源
          最近更新 更多