【问题标题】:Converting ordered dictionaries with different values转换具有不同值的有序字典
【发布时间】:2018-08-29 08:59:14
【问题描述】:

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

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', ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'])])

【问题讨论】:

  • 你能举个例子说明你有什么,你希望得到什么,以及到目前为止你做了什么?我并没有真正得到你需要的东西:S

标签: python


【解决方案1】:

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

对于每个键,您遍历所有值并将它们放入您添加到列表中的单个有序字典中(只要您的输入值列表都具有相同数量的数据,这将起作用):

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', '{}')])]

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    相关资源
    最近更新 更多