简体   繁体   English

比较两个字典键并使用Python中的列表值创建字典

[英]Compare two dictionaries keys and create dictionary with list values in Python

Let's say you have two different dictionaries. 假设您有两个不同的字典。

info_stored = {'a' : 0, 'b' : 2, 'c' : 15}

log_stored = {'dog' : 1, 'a' : 1, 'ted' : 14}

I want to compare these 2 dictionaries to determine if keys are matched. 我想比较这两个字典以确定键是否匹配。 Only 'a' is common in this example. 在此示例中,仅'a'是常见的。

for key in info_stored:
    if key in log_stored:

I want to create a new dictionary with the common key and a list of the values from that common key. 我想用公共密钥和该公共密钥的值列表创建一个新的字典。

common_stored = {'a' : [0, 1]}

How about this: 这个怎么样:

info_stored = {'a' : 0, 'b' : 2, 'c' : 15}

log_stored = {'dog' : 1, 'a' : 1, 'ted' : 14}

common_stored = {k: [] for k in info_stored if k in log_stored}

and then: 接着:

for key in common_stored:
    common_stored[k].extend([info_stored[k], log_stored[k]])
print(common_stored)  # common_stored = {'a' : [0, 1]}

The first step is about creating a dictionary with the common elements as keys and empty lists as values. 第一步是创建一个字典,以公共元素为键,空列表为值。

Finally we modify these empty lists based on the contents of the original dicts. 最后,我们根据原始字典的内容修改这些空列表。

You can even combine the two steps in a single dictionary comprehension as follows: 您甚至可以将两个步骤合并为一个字典,如下所示:

common_stored = {k: [info_stored[k], log_stored[k]] for k in info_stored if k in log_stored}
info_stored = {'a' : 0, 'b' : 2, 'c' : 15}
log_stored = {'dog' : 1, 'a' : 1, 'ted' : 14}

common_stored = {}

# Traverse through info_stored dictionary
for key, val in info_stored.items():
   # Check for key in log_stored dictionary, if found add it to common_stored
   if key in log_stored:
      common_stored[key] = [val, log_stored[key]]

common_stored
{'a': [0, 1]}

a simple solution would be this: 一个简单的解决方案是这样的:

info_stored = {'a' : 0, 'b' : 2, 'c' : 15}

log_stored = {'dog' : 1, 'a' : 1, 'ted' : 14}

result={}


for key in info_stored :
  if key in log_stored :
    result[key]=[ info_stored[key], log_stored[key]]

print (result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个字典列表,并在python中创建一个包含所有键和值的新列表 - Compare two list of dictionaries and create a new list with all keys and values in python Python读取两个字典比较值并创建第三个字典 - Python read two dictionaries compare values and create third dictionary 比较两个字典的键,并使用与另一个字典中的键不匹配的键、值对创建一个字典 - Python - Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python 将Python列表中的键值与多个词典进行比较 - Compare values of keys in Python list with multiple dictionaries 比较python词典列表中不同键的值 - compare values of different keys in list of dictionaries in python 比较两个字典的键和值 - compare two dictionaries' keys and values 比较两个大词典并为它们共有的键创建值列表 - Compare two large dictionaries and create lists of values for keys they have in common 仅在python中的两个字典中比较匹配键的值 - Only compare values of matching keys in two dictionaries in python 如何比较具有相同键的两个字典并使用 python 中的条件更新值? - How to compare two dictionaries with the same keys and update values with a condition in python? 如何比较 python 中具有不同键的两个字典的值 - How to compare values of two dictionaries that have different keys in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM