小编jen*_*nla的帖子

How to search a list with nested dictionary by dictionary value, returning the index of the list with the dictionary element

If I have a list with some nested dictionaries each containing the same keyset and value type set.

list = [{'a': 1, 'b': 2.0, 'c': 3.0}, 
        {'a': 4, 'b': 5.0, 'c': 6.0}, 
        {'a': 7, 'b': 8.0, 'c': 9.0}]
Run Code Online (Sandbox Code Playgroud)

返回 '​​b' 字典键中 5.0 第一次出现的列表索引的最 Pythonic 方法是什么,如果没有找到,则返回 None ?

我知道我可以手动迭代和搜索:

#with the list above...

result = None
for dict in list:
  if dict['b'] == 5.0:
    result = list.index(dict)
    break
print result
#(prints 1)


#as another example, same code but searching for a …
Run Code Online (Sandbox Code Playgroud)

python dictionary list

3
推荐指数
1
解决办法
191
查看次数

标签 统计

dictionary ×1

list ×1

python ×1