且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在字典python中访问列表中的项目

更新时间:2023-11-28 16:42:04

您必须使用for,简单的if不足以检查一组未知的列表:

You'll have to use a for, a simple if is not enough to check an unknown set of lists:

for key in mydict.keys():
    if item in mydict[key]:
        print key

没有显式for语句的方法可能是这样的:

An approach without an explicit for statement would be possible like this:

foundItems = (key for key, vals in mydict.items() if item in vals)

返回与item关联的所有键.但是在内部,仍然存在某种迭代.

which returns all keys which are associated with item. But internally, there's still some kind of iteration going on.