转换具有不同值的有序字典

时间:2018-08-29 08:59:14

标签: python

我有一个有序字典的有序字典,现在如果键的值是列表,我需要使用列表中的所有值来创建字典列表,

dee= OrderedDict([('b', [1,9]), ('a', ['{}','{}'])])
eee=[OrderedDict([('b', 1),('a',{})]),OrderedDict([('b', 9),('a',{})])]

现在我该如何像ab一样递归地进入字典并创建一个包含相同键和不同值的字典的列表。

ab=OrderedDict([('part', ['8888822701B', '8889012006B', '8889012013B', '8889012014B', '8889012016C', '8889012019B', '8889012020C', '8889012021D', '8889012022B']), ('file', ['0', '1', '2', '3', '4', '5', '6', '7', '8']), ('estimatedtime', ['100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100']), ('signature', ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}']), ('otherpartsignature', ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'])])

1 个答案:

答案 0 :(得分:0)

您可以遍历所有键并在处理的第一个键上创建新的Ordered字典,之后的键将添加到较早创建的字典中。

对于每个键,您都要遍历所有值并将它们放入添加到列表中的单个Ordered字典中(只要您输入的值列表都具有相同的数据量,此方法才有效):

from collections import OrderedDict

ab = OrderedDict([
    ('part', ['8888822701B', '8889012006B', '8889012013B', '8889012014B', '8889012016C', 
              '8889012019B', '8889012020C', '8889012021D', '8889012022B']), 
    ('file', ['0', '1', '2', '3', '4', '5', '6', '7', '8']), 
    ('estimatedtime', ['100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100', ' 100']), 
    ('signature', ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}']), 
    ('otherpartsignature', ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'])])

listOfDicts = []
for key,values in ab.items():
    if not dicts: # create all new OrderedDicts when processing the values of first key
        for v in values:
            listOfDicts.append(OrderedDict({key:v}))
    else:
        for idx,v in enumerate(values): # use the earlier created Ordered dicts and add to them
            d = listOfDicts[idx]
            d[key]=v

print(listOfDicts)

输出:

[OrderedDict([('part', '8888822701B'), ('file', '0'), ('estimatedtime', '100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012006B'), ('file', '1'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012013B'), ('file', '2'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012014B'), ('file', '3'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012016C'), ('file', '4'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012019B'), ('file', '5'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012020C'), ('file', '6'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012021D'), ('file', '7'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')]), 
 OrderedDict([('part', '8889012022B'), ('file', '8'), ('estimatedtime', ' 100'), ('signature', '{}'), ('otherpartsignature', '{}')])]