迭代字典列表时避免KeyError

P.h*_*ter 1 python dictionary list-comprehension

我有一个词典列表:

test = [{'first': '1'}, {'second': '2'}, {'first': '0'}, {'third': '3'}, {'fourth': '4'}]
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时:

stuff = [L['first'] for L in test]
print(stuff)
Run Code Online (Sandbox Code Playgroud)

我明白了:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/test_run.py", line 4, in <module>
    stuff = [L['first'] for L in test]
  File "C:/Users/User/Desktop/test_run.py", line 4, in <listcomp>
    stuff = [L['first'] for L in test]
KeyError: 'first'
Run Code Online (Sandbox Code Playgroud)

我知道我可能会犯一个愚蠢的错误,但任何帮助?

Eri*_*nil 6

列表理解+ if

如果你想要所有的值,你需要先检查dict是否有相应的键:

>>> [d['first'] for d in test if 'first' in d]
['1', '0']
>>> [d['sixth'] for d in test if 'sixth' in d]
[]
Run Code Online (Sandbox Code Playgroud)

只有一个值

如果您确定它们至少是一个带有值的dict,您可以使用next获取与第一次出现相对应'first''first'值:

>>> test = [{'first': '1'}, {'second': '2'}, {'first': '0'}, {'third': '3'}, {'fourth': '4'}]
>>> next(d['first'] for d in test if 'first' in d)
'1'
Run Code Online (Sandbox Code Playgroud)

它提出了StopIteration另一个:

>>> next(d['sixth'] for d in test if 'sixth' in d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
Run Code Online (Sandbox Code Playgroud)

替代数据格式

最后,如果经常执行此操作,稍微更改格式可能会很有趣:

from collections import defaultdict
data = defaultdict(list)

test = [{'first': '1'}, {'second': '2'}, {'first': '0'}, {'third': '3'}, {'fourth': '4'}]

for d in test:
    for k in d:
        data[k].append(d[k])

print(data)
# defaultdict(<type 'list'>, {'second': ['2'], 'fourth': ['4'], 'third': ['3'], 'first': ['1', '0']})
print(data['first'])
# ['1', '0']
print(data['sixth'])
# []
Run Code Online (Sandbox Code Playgroud)

for只需要一次循环,查找非常快之后.