简体   繁体   English

如何检查另一个数组中是否存在数组键的值?

[英]How to check if a value of a keys of an array exist in another array?

I can do this: 我可以做这个:

filter(lambda x: x.key1 in ["aa", "bb", "cc"], [{key1: ..., key2: ...}, {key1: ...}])

How can I do the opposite thing? 我怎么能做相反的事情呢?

dict_items = [{key1: ..., key2: ...}, {key1: ...}]
filter(lambda x: x in ???dict_items.key1???, ["aa", "bb", "cc"])

Assuming you have two dicts d1 = {key1: val1, key2: val2 } and d2 = {key3: val3, key4: val4} 假设你有两个dicts d1 = {key1: val1, key2: val2 }和d2 = {key3: val3, key4: val4}

It's not clear what you are trying to compare but if you want to compare keys just type: 目前还不清楚你要比较什么,但如果你想比较键只需输入:

set.intersection(set(d1.keys()), set(d2.keys()))

For values: 对于价值观:

set.intersection(set(d1.values()), set(d2.values()))

use can use list comprehension as follows: 使用可以使用列表理解如下:

a = {"aa":1, "ll":4}
b = {"bb": 'pl', "xx":12, "qq": 66}
print [key for j in [a, b] for key, val in j.iteritems() if key in ["aa", "bb", "cc"]]

Or if you only require keys u can use: 或者,如果您只需要钥匙,您可以使用:

print [key for j in [a, b] for key in j.keys() if key in ["aa", "bb", "cc"]]

output: 输出:

['aa', 'bb']

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

相关问题 如何检查数组中是否存在所有指定的键? - How to check if all specifiedn keys exist in array? 如何检查熊猫中另一个数组中存在的数组中值的百分比? - How do I check the % of values in an array that exist in another array in pandas? 检查数组 - 是否包含在另一个数组中? - Check for array - is value contained in another array? 如何检查一个数组中的元素是否存在于另一个数组中(如果有的话)使用Python打印计数 - How to check if elements in one array exist in another array if so print the count using Python 如何检查 np.array 是否存在于另一个 np.array 中? - How do I check if np.array exist inside another np.array? 如何检查 csv 文件中的值是否存在于另一个文件中 - How to check if a value on a csv file exist in another 如何检查元素的一部分是否存在于python中的数组中? - How to check if part of an element exist in array in python? 如何检查一个数组是否在Python中的另一个数组中 - How to check if an array is in another array in Python 检查dict值是否作为另一个dict中的键存在 - Check if dict values exist as keys in another dict 如何在另一个数组中保存的每个数组中检查特定的精确匹配值? 蟒蛇 - How do i check for a specific exact match value in each array that is held within another array? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM