Python - 方便的方式来检查作为字典一部分的列表中的值? [英] Python - Convenient way to check for a value in a list that is part of a dictionary?

查看:271
本文介绍了Python - 方便的方式来检查作为字典一部分的列表中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承认没有对这个话题进行大量的研究,但是我正在努力快速完成任务。我有一个字典,其中整数为键,列表为值。以前,我正在使用简单的if语句检查字典中的列表:

  if(someList is someDictionary.values ()):
someCode()#failure

但是,我意识到这是不正确的我在做什么,我只想检查列表的第一个值在字典的值中,例如

  if(someList [0] == someValueInDictionary [0]):
someCode()#failure

我第一次尝试

  if(someList [0] in someDictionary.values()[0]):
someCode()#failure

但这显然不起作用。由于someDictionary.values()本身就是一个列表。我意识到我可以迭代所有的值来检查,例如

  for someDictionary.values()中的列表:
if(someList [0] == list [0]):
someCode()#failure
actualCode()#success

但这真的搞砸了我的程序的流程。我是一个新的Python程序员,在Java中最有经验,我正在尝试在我的骨头中获得Python的简洁和方便,因此我认为可能有一个更好的解决方案,我正在测试。如果没有,我可以使迭代的工作,但如果有,我会非常感谢!



提前感谢

解决方案

使用 any()函数与一个生成器表达式,以查找是否有任何字典值包含您的项目:



$ $ $ $ $ $ $ code code code code code code code code $ c $

在Python 3上使用 someDictionary.values()。生成器表达式循环使用字典值而不是首先生成所有值的列表),并对每个值进行测试,因为生成器表达式被迭代过来。



any() 仅测试生成器表达式中的元素,直到一个 True ,然后停止循环,使其相对有效。



如果需要的话要具有匹配值的关键字,请使用 next()

  next((k for k,v in someDictionary.iteritems()if someList [0] in v),None)

其中返回匹配的键,或中没有找到匹配。


I have admittedly not done a huge amount of research on this topic, but I am trying to get something done quickly. I have a dictionary with integers as keys and lists as values. Previously, I was checking for a list being in the dictionary with a simple if statement:

if(someList is in someDictionary.values()):
    someCode() #failure

However, I realized it is incorrect for what I was doing, and I only want to check for the inclusion of the first value of the list in the dictionary's values, e.g

if(someList[0] == someValueInDictionary[0]):
    someCode() #failure

I first tried

if(someList[0] is in someDictionary.values()[0]):
    someCode() #failure

But that clearly doesn't work. As someDictionary.values() is a list in itself. I realize I could iterate through all of the values to check, e.g

for list in someDictionary.values():
    if(someList[0] == list[0]):
        someCode() #failure
actualCode() #success

But this really messes up the flow of my program. I am a new Python programmer, most experienced in Java, and I am trying to get the conciseness and convenience of Python in my bones, as such I thought there might be a better solution for what I am testing for. If there is not, I can make the iteration thing work, but if there is, I would greatly appreciate it!

Thanks in advance!

解决方案

Use the any() function with a generator expression to find if there is any dictionary value that contains your item:

if any(someList[0] in v for v in someDictionary.itervalues()):
    # item found

Use someDictionary.values() on Python 3. The generator expression loops over the dictionary values (without producing a list of all values first) and tests against each value, one by one as the generator expression is iterated over.

any() only tests elements from the generator expression until one is True, and then stops looping, making this relatively efficient.

If you need to have the key of the matching value, use next():

next((k for k, v in someDictionary.iteritems() if someList[0] in v), None)

which returns either the matching key, or None in no match is found.

这篇关于Python - 方便的方式来检查作为字典一部分的列表中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆