我有字典的Python列表如下:檢查字典列表中是否存在值?
a = [
{'main_color': 'red', 'second_color':'blue'},
{'main_color': 'yellow', 'second_color':'green'},
{'main_color': 'yellow', 'second_color':'blue'},
]
我想檢查與特定鍵/值字典中是否已經存在在列表中,如下所示:
// is a dict with 'main_color'='red' in the list already?
// if not: add item
很不錯的。謝謝! – AP257 2010-10-09 19:29:37
令人驚歎的單線語法,我一直都很努力!我很好奇,在Python文檔中向我們展示的地方,我們實際上可以在'for'之前放置'for'的操作? – sylye 2017-05-11 11:26:39
我發現它,它被稱爲List Comprehensions https://docs.python.org/2/whatsnew/2.0.html?highlight=comprehensions – sylye 2017-05-12 10:01:38