如何将字典追加到循环列表中

时间:2012-01-20 20:15:08

标签: python dictionary

我以一系列元组的形式处理大量数据。每个元组都有一个指定的格式,如(a, b, c, d, e)。元组列表如下所示:

tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
             ('a2', 'b2', 'c2', 'd2', 'e2'),
             ...
             ('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]

我想要的是,将每个元组转换为字典,并将字典附加到最终的字典列表中。这一切都可以循环完成吗?最终的词典列表应如下所示:

finalDictList = [{'key1': 'a1', 'key2': 'b1', 'key3': 'c1', 'key4': 'd1', 'key5': 'e1'},
                 {'key1': 'a2', 'key2': 'b2', 'key3': 'c2', 'key4': 'd2', 'key5': 'e2'},
                 {'key1': 'a3', 'key2': 'b3', 'key3': 'c3', 'key4': 'd3', 'key5': 'e3'},
                 ...
                 {'key1': 'a10000', 'key2': 'b10000', 'key3': 'c10000', 'key4': 'd10000', 'key5': 'e10000'}]

元组的格式是固定的。我想比较一个字典的每个键与所有其他键的后缀,值。这就是为什么将元组转换为字典对我来说是有意义的。如果设计范式本身看起来不对劲,请纠正我。此外,还有> 10000个元组。声明许多字典都没有完成。

是否有将字典附加到循环中的列表?此外,如果可以,我们可以通过它的关键值来访问每个字典,比如finalDictList[0]['key1']吗?

7 个答案:

答案 0 :(得分:10)

我们将混合三个重要概念,使这段代码非常小巧漂亮。首先是list comprehension,然后是zip方法,最后是dict方法,用于从元组列表中构建字典:

my_list = [('a1', 'b1', 'c1', 'd1', 'e1'), ('a2', 'b2', 'c2', 'd2', 'e2')]
keys = ('key1', 'key2', 'key3', 'key4', 'key5')
final = [dict(zip(keys, elems)) for elems in my_list]

之后,final变量的值为:

>>> final
[{'key3': 'c1', 'key2': 'b1', 'key1': 'a1', 'key5': 'e1', 'key4': 'd1'},
{'key3': 'c2', 'key2': 'b2', 'key1': 'a2', 'key5': 'e2', 'key4': 'd2'}]

此外,您可以使用列表中字典的位置和您要查找的密钥来获取某个字典的元素,即:

>>> final[0]['key1']
'a1'

答案 1 :(得分:6)

使用zip将预定义的键名列表与输入列表中的每个元组组合,然后将结果传递给dict以使其成为dicts。将整个事物包含在列表理解中,以便一次性处理它们:

keys = ('key1', 'key2', 'key3', 'key4', 'key5')
finalDictList = [dict(zip(keys, values)) for values in tupleList]

答案 2 :(得分:4)

当你已经有一个元组列表时,我不确定为什么你需要将所有东西都转换成字典。

>>> tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
...              ('a2', 'b2', 'c2', 'd2', 'e2'),
...              ('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]
>>> [x[1] for x in tupleList]
['b1', 'b2', 'b10000']

使用Python的 list comprehension 语法,您可以获得每个元组的所有第n个元素的列表。

答案 3 :(得分:3)

如果字段已修复,您可以执行以下操作:

fields = ['key1', 'key2', 'key3', 'key4', 'key5']

newList = [dict(zip(fields, vals)) for vals in oldList]

答案 4 :(得分:2)

如果你说你有很多条目,请记住python有namedtuples

>>> tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
...              ('a2', 'b2', 'c2', 'd2', 'e2'),
...              ('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]
>>>
>>> from collections import namedtuple
>>> fv = namedtuple('fivevals', ('key1', 'key2', 'key3', 'key4', 'key5'))
>>> tuplelist = [fv(*item) for item in tupleList]
>>> 
>>> tuplelist[0].key1
'a1'
>>>

可以通过键接收Namedtuples,但同时它们是轻量级的,并且不需要比常规元组更多的内存。

答案 5 :(得分:0)

finalDictList = []
for t in tupleList:
    finalDictList.append({
        'key1': t[0],
        'key2': t[1],
        'key3': t[2],
        'key4': t[3],
        'key5': t[4],
    })
  

另外,如果可以的话,我们可以通过它的关键值访问每个字典,比如finalDictList [0] ['key1']吗?

当然,这正是你要做的。

答案 6 :(得分:0)

from itertools import izip

keys = ['key1', 'key2', 'key3', 'key4', 'key5']
finalDictList = [dict(izip(names, x)) for x in tupleList]
相关问题