比方说有是有弦与名单混合的字典:嵌套列表理解与用条件
dictionary = {'item_a': 'one',
'item_b': 'two',
'item_c': ['three', 'four'],
'item_d': 'five'}
,其结果应该是:
['one', 'two', 'three', 'four', 'five']
它如何可以实现通过使用列表理解?
下面给出了仅用于列表中的值,但它缺少不在列表中的字符串和其他不工作,如果加权后,如果:
[val for sublist in dictionary.values() if type(sublist) is list for val in sublist]
我想答案你可能正在寻找可以在这里找到:https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python – Fallenreaper
可能的重复[制作一个简单的列表从Python列表中删除列表](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – Fallenreaper
你将会遇到问题,因为字典是无序的数据结构。 – GWW