所以我的目标是从:
fruitColourMapping = [{'apple': 'red'}, {'banana': 'yellow'}]
finalMap = {'apple': 'red', 'banana': 'yellow'}
一种方法我得到了: from itertools import chain
fruits = list(chain.from_iterable([d.keys() for d in fruitColourMapping]))
colour = list(chain.from_iterable([d.values() for d in fruitColourMapping]))
return dict(zip(fruits, colour))
有没有更好的、更符合Python风格的方法?
fruit
和color
列表一样)只是侥幸生成了两个正确顺序的值集合。 - Silas Ray