【问题标题】:Iterate two dictionaries and create a new dictionary where keys are not matching迭代两个字典并创建一个键不匹配的新字典
【发布时间】:2015-11-30 05:29:54
【问题描述】:

我想合并以下两个字典的内容

>>> key_dict 
{    
     '1': 'seq', 
     '0': 'seq',
     '2': 'seq'
}
>>> value_dict
{    
     'tedious; repetitive and boring': 'text',
     'worldly': 'text', 
     'ordinary': 'text'
}

所以在最终列表中我有类似的东西

>>>final_list 
[
    {
        'seq': 0,
        'text': 'worldly'
    }, 
    {
        'seq': 1, 
        'text': 'tedious; repetitive and boring'
    },
    {
        'seq': 2, 
        'text': 'ordinary'
    }
]

编辑: 抱歉,我试过这个

>>> final_list = []
>>> temp_dict = {}
>>> for (key1, value1), (key2, value2) in zip(key_dict.items(), value_dict.items()):
...   temp_dict[value1] = key1
...   temp_dict[value2] = key2
...   final_list.append[temp_dict]
... 
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable
>>> 

任何关于我哪里出错的帮助

编辑 2

改正后final_list.append[temp_dict]改成final_list.append(temp_dict)

我明白了

>>> pprint(final_list)
[{'seq': '1', 'text': 'ordinary'},
 {'seq': '1', 'text': 'ordinary'},
 {'seq': '1', 'text': 'ordinary'}]
>>>

谁能解释为什么这些值会重复,我无法理解。

【问题讨论】:

  • 抱歉,我编辑了问题并添加了到目前为止我尝试过的内容
  • 为了让你所有的字典都必须是OrderedDict
  • 我应该将值作为元组传递给dicts 吗?
  • 解决你的即时错误final_list.append[temp_dict]应该是final_list.append(temp_dict)
  • @PaulRooney Dang。我在问题中添加了输出

标签: python list python-3.x dictionary


【解决方案1】:

你实际上是在正确的轨道上。值是重复的,因为您没有在每次迭代结束时清空 temp_dict。事实上,你甚至根本不需要temp_dict,只需这样做:

>>> for (key1, value1), (key2, value2) in zip(key_dict.items(), value_dict.items()):
...     final_list.append({value1: int(key1), value2: key2})

[{'seq': 1, 'text': 'ordinary'},
 {'seq': 0, 'text': 'tedious; repetitive and boring'},
 {'seq': 2, 'text': 'worldly'}]

如果顺序很重要,那么您应该使用collections.OrderedDict 而不是普通的字典。

【讨论】:

    【解决方案2】:

    按照问题中指定的顺序

    from collections import  OrderedDict
    from pprint import pprint
    
    key_dict = {    
         '1': 'seq', 
         '0': 'seq',
         '2': 'seq'
    }
    value_dict = {    
         'tedious; repetitive and boring': 'text',
         'worldly': 'text', 
         'ordinary': 'text'
    }
    
    kds = OrderedDict(sorted(kv for kv in key_dict.items()))
    vds = OrderedDict(sorted((kv for kv in value_dict.items()), reverse=True))
    
    d = [{v1: int(k1), v2: k2} for (k1, v1), (k2, v2) in zip(kds.items(), vds.items())]
    
    pprint(d)
    

    输出:

    [{'seq': 0, 'text': 'worldly'},
     {'seq': 1, 'text': 'tedious; repetitive and boring'},
     {'seq': 2, 'text': 'ordinary'}]
    

    可以通过将temp_dict 移动到循环中来解决原始代码的问题。
    例如

    final_list = []
    for (key1, value1), (key2, value2) in zip(key_dict.items(), value_dict.items()):
        temp_dict = {}
        temp_dict[value1] = key1
        temp_dict[value2] = key2
        final_list.append(temp_dict)
    

    【讨论】:

    • 很好的答案:)。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多