简体   繁体   English

如何将列表值与字典键进行比较并使用 python 创建一个新字典

[英]How to compare list values with dictionary keys and make a new dictionary of it using python

I have a list like this:我有一个这样的清单:

lis = ['Date', 'Product', 'Price']

I want to compare it with:我想将它与:

dict = {'Date' : '2013-05-01', 'Salary' : '$5000', 'Product' : 'Toys', 'Price' : '$10', 'Salesman' : 'Smith'}

I want to compare each item of list with keys of dictionary and make a new dictionary.我想将列表的每个项目与字典的键进行比较并制作一个新字典。
What I have tried is:我尝试过的是:

n = {}
for k,v in dict.items():
    for i in lis:
        if i==k:
            n[k] = v

Output:输出:

n = {'Date' : '2013-05-01', 'Product' : 'Toys', 'Price' : '$10'}

This works but I want to do it through generators - can someone help me do that?这有效,但我想通过生成器来完成 - 有人可以帮我做吗?

Treat lis as a set instead, so you can use dictionary views and an intersection:lis视为一个集合,因此您可以使用字典视图和交集:

# python 2.7:
n = {k: d[k] for k in d.viewkeys() & set(lis)}

# python 3:
n = {k: d[k] for k in d.keys() & set(lis)}

Or you could use a simple dict comprehension with a in test against d :或者你可以使用一个简单的 dict 理解和 a ind测试:

# python 2.6 or older:
n = dict((k, d[k]) for k in lis if k in d)

# python 2.7 and up:
n = {k: d[k] for k in lis if k in d}

This presumes that not all values in lis are going to be in d ;这假定并非lis中的所有值都将在d the if k in d test can be dropped if they are always going to be present. if k in d测试中的if k in d始终存在,则可以删除它们。

For your specific case, the second form is quite a lot faster:对于您的具体情况,第二种形式要快得多:

>>> from timeit import timeit
>>> timeit("{k: d[k] for k in d.viewkeys() & s}", 'from __main__ import d, lis; s=set(lis)')
2.156520128250122
>>> timeit("{k: d[k] for k in lis if k in d}", 'from __main__ import d, lis')
0.9401540756225586
filtered_dict = dict((k, original_dict[k]) for k in lis if k in original_dict)

Or if you have 2.7+:或者,如果您有 2.7+:

filtered_dict = {k: original_dict[k] for k in lis if k in original_dict}

If you want to use a generator:如果要使用生成器:

item_generator = ((k, original_dict[k]) for k in lis if k in original_dict)

The generator will yield (key, value) pairs.生成器将产生(key, value)对。

For Python 3.6对于 Python 3.6

dict_keys_list = list(dict.keys())
dict_keys_list.sort()
for key in dict_keys_list:
     if key in lis:
          print("Key is present")

Another approach where you want to compare a list of keys to be equal to the dictionary keys.您希望将键列表与字典键进行比较的另一种方法。

person = { 'name' : 'Don', 'nickname' : 'John', 'age' : 45, 'salary' : 50000, 'place' : 'Monaco, Europe' }
compare_list = ['nickname', 'place', 'name', 'salary', 'age']
person_keys_list = list(person.keys())
person_keys_list.sort()
compare_list.sort()
person_keys_list == compare_list # Gives you True

暂无
暂无

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

相关问题 如何使用python比较字典的值并为答案创建新的字典 - How to compare values of a dictionary and make a new dictionary for the answers using python Python - 如何通过键和返回值将列表与字典进行比较 - 然后放入列表 - Python - How to compare list with dictionary by keys and return values - put then into a list Python词典:将键与所有键的值进行比较 - Python Dictionary: Compare keys with Values of All Keys 比较两个字典键并使用Python中的列表值创建字典 - Compare two dictionaries keys and create dictionary with list values in Python 如何用Python中的键列表和值字典创建字典? - How to create a dictionary from a list of keys and a dictionary of values in Python? 使用 python 将字典列表值与列表值列表进行比较 - Compare dictionary list values to the list of list values using python 比较字典与列表的键 - Compare keys of dictionary with list 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? Python:如何比较字典中不同键的值,然后删除重复项? - Python: How to compare values of different keys in dictionary and then delete duplicates? 如何使用 Python 中的特定键从字典列表中创建字典 - How to create a dictionary from a list of dictionary using specific keys in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM