【问题标题】:Efficient mapping between first element of each tuple in a list and the alphabet characters列表中每个元组的第一个元素与字母字符之间的有效映射
【发布时间】:2017-05-22 02:06:50
【问题描述】:

我有一个字符串元组列表:

lst = [('first', 'one'), ('second', 'two'), ('third', 'three'), ('fourth', 'four')]

我想在每个元组的第一个元素和英文字母小写字符之间创建一个映射:

  • 'first' 映射到 'a'
  • 'second' 映射到 'b'
  • 'third' 映射到 'c'
  • 'fourth' 映射到 'd'

我试过字典

dct = {'first': 'a', 'second': 'b', 'third': 'c', 'fourth': 'd'}

但我想知道是否有更有效的方法可以创建包含元组的第一个元素和字母列表的列表,然后迭代它们以创建字典。

另外,对于字母字符,我尝试过使用string.ascii_lowercase,但它给出的字符串不是每个字符的列表。

我是 Python 新手,如果上面的问题是基本的,请原谅。如果有任何示例代码供我理解和学习,我将不胜感激。

【问题讨论】:

    标签: python list python-3.x dictionary tuples


    【解决方案1】:

    您可以使用zip() 和字典comprehension

    >>> from string import ascii_lowercase
    >>> lst = [('first', 'one'), ('second', 'two'), ('third', 'three'), ('fourth', 'four')]
    >>> {x[0]: y for x, y in zip(lst, ascii_lowercase)}
    {'first': 'a', 'second': 'b', 'third': 'c', 'fourth': 'd'}
    

    或者如果您想以更“实用”的方式进行操作:

    >>> from operator import itemgetter
    >>> {x: y for x, y in zip(map(itemgetter(0), lst), ascii_lowercase)}
    {'first': 'a', 'second': 'b', 'third': 'c', 'fourth': 'd'}
    

    【讨论】:

    • 很好的答案。能否请您对这两种方式在效率方面的差异进行一个小解释,并解释mapitemgetter功能?
    • 我几乎无法比map()itemgetter() 的文档更好地解释它们。至于效率,第一个版本会更高效,因为它涉及的函数调用更少。
    【解决方案2】:
    import string 
    lst = [('first', 'one'), ('second', 'two'), ('third', 'three'), ('fourth', 'four')]
    alphabetList = [i for i in string.ascii_lowercase ]
    dic = {lst[i] : alphabetList[i] for i in range(len(lst))}
    print(dic)
    

    【讨论】:

      【解决方案3】:

      当然,您可以使用字典理解和使用 string 库来实现:

      from string import ascii_lowercase
      
      {lst[i][0]: c for i, c in enumerate(ascii_lowercase)}
      

      【讨论】:

        【解决方案4】:

        您可以使用zip 创建您想要的dict

        from string import ascii_lowercase
        lst = [('first', 'one'), ('second', 'two'), ('third', 'three'), ('fourth', 'four')]
        
        my_dict =  dict(zip(list(zip(*lst))[0], ascii_lowercase)) # in python 3
        # In python 2, you may just do:
        #     my_dict = dict(zip(zip(*lst)[0], ascii_lowercase))
        

        或者,最好将它与operator.itemgetter 一起使用:

        my_dict = dict(zip(map(itemgetter(0), lst), ascii_lowercase))
        

        my_dict 持有的价值将是:

        {'first': 'a', 'second': 'b', 'third': 'c', 'fourth': 'd'} 
        

        说明:

        你快到了。 string.ascii_lowercase 返回一个字符串,但您可以迭代字符串并根据索引访问项目 (与 list 相同)。例如:

        my_alphas = string.ascii_lowercase
        
        # Iterate the string, valid
        for alpha in my_alphas:
            print alpha
        
        # access based on index, valid
        my_alphas[13]
        

        作为附加信息,即使您不需要它:如果您想将 string 转换为 list,您可以将其类型转换为:

        my_alpha_list = list(string.ascii_lowercase)
        #  ^  will hold all the alphabets in the form of list
        

        【讨论】:

        • 感谢您的解释。但是,我得到一个“zip”对象是不可订阅的,并且为了学习 Python,我想知道为什么如果你能弄清楚的话。谢谢
        • @Karim 我的错。我还没有看到 Python 3 标签。在 python 3 中,zip 返回zip 类型的对象,您必须将其显式类型转换为列表才能使其工作。更新了答案
        • 感谢您的更新。您是否还关心添加itemgetter 方法的小解释;为什么它更好以及它有何不同?
        猜你喜欢
        • 2020-06-21
        • 1970-01-01
        • 2012-05-21
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 2021-05-17
        相关资源
        最近更新 更多