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)