简体   繁体   English

python-如何从字典列表中确定找到的两个键是否属于同一字典

[英]python - how to determine if two keys found belong to same dict from list of dicts

I have a list of dicts where I'm trying to find two keys and if found make sure they belong to the same dictionary. 我有一个字典列表,我试图在其中找到两个键,如果找到,请确保它们属于同一词典。 What I have so far: 到目前为止,我有:

foo = [{'apples': 1, 'banana': 2}, {'people': 1, 'monkeys': 2}]

food = any(d['apples'] == 1 for d in foo)
mammals = any(d['banana'] == 2 for d in foo)

if food and mammals:
    return True

But how do I verify they were both from the same dictionary? 但是,如何验证它们都来自同一词典?

Just check both keys at once then there is no need for any other checks : 只需一次检查两个键,则无需进行其他任何检查:

foo = [{'apples': 1, 'banana': 4}, {'people': 1, 'monkeys': 2}]
print(any(d.get('apples') == 1 and d.get('banana')== 2  for d in f))

So simply: 如此简单:

 return any(d.get('apples') == 1 and d.get('banana')== 2  for d in f)

As it stands you would actually get a keyError , you should use dict.get to catch when any of the keys are missing in each dict . 就目前而言,您实际上会得到一个keyError ,应该使用dict.get来捕获每个dict中缺少任何键的时间。

Good first attempt, try this: 很好的第一次尝试,试试这个:

foo = [{'apples': 1, 'banana': 2}, {'people': 1, 'monkeys': 2}]

food = [('apples' in d and 'bannana' in d) for d in foo]

If I understand your verbal description, then the list "foo" will now contain items "True" or "False", indicating whether each dictionary contains both keys. 如果我理解您的口头描述,那么列表“ foo”现在将包含项目“ True”或“ False”,指示每个词典是否都包含两个键。 To see this is true for any dictionary in the collection, use the "any" function, as before: 要查看集合中的任何词典都是如此,请像以前一样使用“ any”函数:

contained_and_in_same_dic = any(food)

To make use of lazy evaluation, we could switch from a list to a generator and write: 为了使用惰性评估,我们可以从列表切换到生成器并编写:

contained_and_in_same_dic = any( (('apples' in d and 'bannana' in d) for d in foo) )

The plus side here is that it can break out of evaluation as soon as it finds a match, though this will only be noticeable for very large lists 有利的一面是,一旦找到匹配项,它就可以退出评估,尽管这仅在非常大的列表中才明显

Note: your solution does not match your verbal description. 注意:您的解决方案与您的口头描述不符。 The "if == 1" will raise an exception if the key is not found at all. 如果根本找不到密钥,则“ if == 1”将引发异常。

暂无
暂无

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

相关问题 如何将来自两个不同字典的键组合成一个列表,该列表是第三个字典的值 - How to combine the keys from two different dicts into a list which is the value of a third dict 如何通过使用公共密钥对值进行求和,从一系列dicts创建单个Python dict? - How to create single Python dict from a list of dicts by summing values with common keys? Python-比较两个字典列表并从一个列表中返回字典 - Python - Compare two lists of dict and return dicts from one list 如何从 Dicts 列表中创建 Dicts,其中键从列表中的 Dict Keys 中缩回,同时保持不同的值 - How to create a Dicts From a List of Dicts which the Keys are retracted from Dict Keys inside the list while keeping different values 将字典的json列表中的值插入到MYSQL数据库的字典列表中,该字典包含两个不同值的相同键的相同键 - Insert values from a json list of dicts containing the same keys for two different values in the list of dicts to a MYSQL database python 从列表中搜索字典中的两个键 - python search two keys in dict from list 在Python中,如何将不同的项目添加到另一个字典的字典列表中? - In Python, how to add distinct items to a list of dicts from another dict? 在嵌套字典列表中获取相同字典键的平均值 - Getting the average of the same dict keys in a list of nested dicts 使用“ dict of dicts”或“ dicts list”将CSV中的数据存储在python中? - Use “dict of dicts” or “list of dicts” for storing data from CSV in python? 从具有不同键但相同ID的现有字典创建新的python字典 - Creating a new python dict from existing dicts with different keys but same ids
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM