【发布时间】:2018-01-11 11:03:45
【问题描述】:
是否可以将 dict 中的列表组合成新键? 例如,我有一个 dict 设置
ListDict = {
'loopone': ['oneone', 'onetwo', 'onethree'],
'looptwo': ['twoone', 'twotwo', 'twothree'],
'loopthree': ['threeone', 'threetwo', 'threethree']}
我想要一个名为“loopfour”的新键,其中包含来自“loopone”、“looptwo”和“loopthree”的列表
所以它的列表看起来像
['oneone', 'onetwo', 'onethree', 'twoone', 'twotwo', 'twothree', 'threeone', 'threetwo', 'threethree']
并且可以使用 ListDict['four'] 调用并返回组合列表
【问题讨论】:
-
ListDict['loopfour'] = [el for l in ListDict.values() for el in l]. -
列表是可变的。您想如何处理列表中的值发生变化的情况?
标签: python list dictionary append add