당신은 next
을 사용할 수 있습니다 (당신이 첫 번째 신경 또는 대안 있음) 가장 한 경기에서 거기 알고있는 경우 : 다음
>>> loaded_data = [{"acronym": "AUP", "meaning": "Always Use Python"}, {"acronym": "GNDN", "meaning": "Goes Nowhere, Does Nothing"}]
>>> next(d for d in loaded_data if d['acronym'] == 'AUP')
{'acronym': 'AUP', 'meaning': 'Always Use Python'}
그리고 당신이 같은 예외 또는 None
을할지 여부에 따라 - 발견되지 값 : 당신이 원하는 경우
>>> next(d for d in loaded_data if d['acronym'] == 'AZZ')
Traceback (most recent call last):
File "<ipython-input-18-27ec09ac3228>", line 1, in <module>
next(d for d in loaded_data if d['acronym'] == 'AZZ')
StopIteration
>>> next((d for d in loaded_data if d['acronym'] == 'AZZ'), None)
>>>
당신도 직접 값이 아닌 DICT를 얻을 수 :
>>> next((d['meaning'] for d in loaded_data if d['acronym'] == 'GNDN'), None)
'Goes Nowhere, Does Nothing'
출처
2014-01-11 20:12:12
DSM
완벽, 그게 내가 찾고 있던 바로 그거야 !! 많은 많은 감사합니다! :) – Crista23
@ Crista23 나는 질문을 업데이트했기 때문에 기쁘다.'any'에 대한 필요성은 없다. –
그러나 첫 번째로 일치하는 사전이 발견 된 후에도 루핑을 계속한다. – RemcoGerlich