2013-03-16 114 views

回答

8

可以只使用一個list comp - 這是一種用於循環我猜:

>>> d = [ {'file': 'test.py', 'revs': [181449, 181447]}, 
{'file': 'test2.py', 'revs': [4321, 1234]} ] 
>>> [el['file'] for el in d] 
['test.py', 'test2.py'] 

如果不使用的話for,你可以使用:

>>> from operator import itemgetter 
>>> map(itemgetter('file'), d) 
['test.py', 'test2.py'] 

,或在不進口:

>>> map(lambda L: L['file'], d) 
['test.py', 'test2.py'] 
+3

+1,但痘打字比我快。 ;) – 2013-03-16 00:36:52

+0

這就是我正在尋找的。謝謝。 – imagineerThat 2013-03-16 00:36:53

+0

地圖實際上只是表明for循環的另一種方式。列表理解是要走的路。 – 2013-03-16 00:39:31

相關問題