![](/img/trans.png)
[英]Python: Iterate Over Dictionary that Contains Lists and Store the Keys and Values in a String
[英]Remove Keys in Dictionary and Store Values in a Dictionary of Lists
因此,我有一本带有“项目”的字典,其中包含字典列表。 我正在尝试将其重组为“项”的字典,该字典具有包含上一本字典键的值的列表列表。
原版的:
data = {
"items": [
{ "A": 0.00, "B": 33.27, "C": "string", "D": "16122 " },
{ "A": 0.00, "B": 5176.66, "C": "string", "D": "21216 " }
]
}
我想得到什么:
data = {
"items": [
[ 0.00, 33.27, "string", "16122 " ],
[ 0.00, 5176.66, "string", "21216 " ]
]
}
似乎operator.itemgetter
几乎是您想要的:
getter = operator.itemgetter('A', 'B', 'C', 'D')
data = {'items': [getter(dct) for dct in data['items']]}
在这种情况下,你最终有一个list
的tuple
,而不是一个list
的list
,但在许多应用中,这可能是确定。
演示:
>>> data = {
... "items": [
... { "A": 0.00, "B": 2184.83, "C": "string", "D": "16122 " },
... { "A": 0.00, "B": 5176.66, "C": "string", "D": "21216 " }
... ]
... }
>>> import operator
>>> getter = operator.itemgetter('A', 'B', 'C', 'D')
>>> data = {'items': [getter(dct) for dct in data['items']]}
>>> data['items'][0]
(0.0, 2184.83, 'string', '16122 ')
>>> data['items'][1]
(0.0, 5176.66, 'string', '21216 ')
这是一种完全按照您想要的方式进行操作的方法。
#Get the column names from the first record
colNames =data['items'][0].keys()
#Get values from all records that have the same keys as in the first record
newData = { 'items' : [[record[colName] for colName in colNames] \
for record in data['items']] }
print newData
输出:
{'items': [[0.0, 'string', 33.27, '16122 '], [0.0, 'string', 5176.66, '21216 ']]}
请记住,字典是无序的-因此,当映射到列表时,需要指定键的顺序以获得值的相关顺序。 键的顺序不一定是声明的顺序,上次查看的顺序等等。
因此,更实际的示例数据是:
data = {
"items": [
{ "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" },
{ "B": 5176.66, "A": 0.00, "D": "21216 ", "C": "string" }
]
}
要将无序键映射到有序列表,您需要选择要使用的顺序。 假设您按照以下顺序排列顺序:
ordered_keys=("A", "B", "C", "D")
然后,您可以通过简单的循环转换为结构:
for k, LoD in data.items(): # consider '.iteritems() on Py 2 and larger dicts...
data[k]=[[di[sk] for sk in ordered_keys] for di in LoD]
>>> data
{'items': [[0.0, 33.27, 'string', '16122 '], [0.0, 5176.66, 'string', '21216 ']]}
现在,您需要决定如何处理字典列表中可能缺少的键。 除非每个字典具有完全相同的键,否则您需要一个默认值。
这是您可以执行此操作的方法:
data = {
"items": [
{ "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" },
{ "B": 5176.66, "A": 0.00, "D": "21216 ", "C": "string" },
{ "E": "New Key ", "C": "'A' and 'B' are missing in this dict" }
]
}
for k, LoD in data.items():
keys=sorted({e for sk in LoD for e in sk})
data[k]=[keys]+[[di.get(sk, None) for sk in keys] for di in LoD]
在这种情况下,字典列表中的所有键均被收集,排序,然后成为列表列表中的第一个元素(因此您知道哪个data
键和其他data
键可能具有不同的键集。):
data = {
"items": [
{ "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" },
{ "B": 5176.66, "A": 0.00, "D": "21216 ", "C": "string" },
{ "E": "New Key ", "C": "'A' and 'B' are missing in this dict" }
],
"More": [
{ "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" }
]
}
for k, LoD in data.items():
keys=sorted({e for sk in LoD for e in sk})
data[k]=[keys]+[[di.get(sk, None) for sk in keys] for di in LoD]
结果:
>>> for k in data:
... print k+':'+'\n\t'+'\n\t'.join(repr(e) for e in data[k])
items:
['A', 'B', 'C', 'D', 'E']
[0.0, 33.27, 'string', '16122 ', None]
[0.0, 5176.66, 'string', '21216 ', None]
[None, None, "'A' and 'B' are missing in this dict", None, 'New Key ']
More:
['A', 'B', 'C', 'D']
[0.0, 33.27, 'string', '16122 ']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.