【问题标题】:Given two list of strings, how can I convert them into a into a dict?给定两个字符串列表,如何将它们转换为字典?
【发布时间】:2019-06-10 01:59:55
【问题描述】:

我有以下字符串列表:

content = [['a list with a lot of strings and chars 1'], ['a list with a lot of strings and chars 2'], ['a list with a lot of strings and chars 3'], ['a list with a lot of strings and chars 4']]

labels = ['label_1','label_2','label_3','label_4']

如何从它们创建字典:

{
'label_1': ['a list with a lot of strings and chars 1']
'label_2': ['a list with a lot of strings and chars 2']
'label_3': ['a list with a lot of strings and chars 3']
'label_4': ['a list with a lot of strings and chars 4']
}

【问题讨论】:

    标签: python python-3.x data-structures list-comprehension


    【解决方案1】:
    dictionary = dict(zip(labels, content))
    

    各种版本:

    def f1(labels, content):
        return dict(zip(labels, content))
    
    def f2(labels, content):
        d = {}
    
        for i, label in enumerate(labels):
            d[label] = content[i]
        return d
    
    def f3(labels, content):
        d = {}
        for l, c in zip(labels, content):
            d[l] = c
        return d
    
    def f4(labels, content):
        return {l : c for (l, c) in zip(labels, content)}
    
    def f5(labels, content):
        dictionary = {}
    
        for i in range(len(content)):
           dictionary[labels[i]] = content[i]
        return dictionary
    
    def f6(labels, content):
        return {l : content[i] for (i, l) in enumerate(labels)}
    

    时间

    这些是使用 Python 3.6.7 测试的。请注意,不同版本的 Python 可能具有不同的性能,因此您可能应该在目标平台上重新运行基准测试。

    In [20]: %timeit f1(labels, content)
    637 ns ± 4.17 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    In [21]: %timeit f2(labels, content)
    474 ns ± 4.44 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    In [22]: %timeit f3(labels, content)
    447 ns ± 2.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    In [23]: %timeit f4(labels, content)
    517 ns ± 4.44 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    In [24]: %timeit f5(labels, content)
    529 ns ± 8.04 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    In [4]: %timeit f6(labels, content)
    602 ns ± 0.64 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    

    最快

    最快的是f3,@Michael_MacAskill 修改答案以使用zip 而不是使用索引从content 中提取值。

    有趣的是,对@Michael_MacAskill 答案的字典理解并没有比使用普通 for 循环的结果更好。也许该语言的实现者意识到人们大部分时间仍然坚持使用 for 循环,并为他们实施了一些性能改进。

    大多数 Pythonic

    如果速度差异并不重要,大多数有经验的 Python 程序员可能会选择 dict(zip(labels, content)) 选项,因为它是语言中的常见习语。

    【讨论】:

      【解决方案2】:
      dictionary = {}                                                         
      
      for i in range(len(content)): 
         dictionary[labels[i]] = content[i] #setting each element in labels list as key and each corresponding index of content list's content as value
      

      【讨论】:

        【解决方案3】:

        也许这可以通过字典理解更有效地完成,但这是一种快速而肮脏的方法:

        d = {}                                                                                                                
        
        for i, label in enumerate(labels): 
            d[label] = content[i] 
        

        【讨论】:

        • 实际上@lightalchemist 的答案要优雅得多。
        • 哪个答案最有效?
        • 这取决于您的效率指标,但如果只是“键入的代码行”,我当然会选择@lightalchemist 作为公认的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 2011-07-12
        • 2014-05-23
        • 2019-05-29
        相关资源
        最近更新 更多