从python中的字典转换为列表的问题[重复]
Posted
技术标签:
【中文标题】从python中的字典转换为列表的问题[重复]【英文标题】:Issue in converting to list from dictionary in python [duplicate] 【发布时间】:2019-12-14 17:43:41 【问题描述】:我已经将我的字典转换为列表,但在获取特定格式时卡住了。我的输出字典是,
foldersinlist = [['Training'], ['Training videos', 'Training documents', 'Training workouts', 'Training practicals', 'Training practicals', 'Training practicals']]
我想要的,
['Training', 'Training videos', 'Training documents', 'Training workouts', 'Training practicals', 'Training practicals', 'Training practicals']
我目前拥有的代码如下,
foldersinlist = list(folders.values())
print(foldersinlist)
【问题讨论】:
【参考方案1】:这是嵌套列表而不是字典:
foldersinlist = [['Training'], ['Training videos', 'Training documents', 'Training workouts', 'Training practicals', 'Training practicals', 'Training practicals']]
这是平面列表:
['Training', 'Training videos', 'Training documents', 'Training workouts', 'Training practicals', 'Training practicals', 'Training practicals']
只是做:
ll = sum(foldersinlist , [])
【讨论】:
【参考方案2】:试试:
print([x for i in foldersinlist for x in i])
输出:
['Training', 'Training videos', 'Training documents', 'Training workouts', 'Training practicals', 'Training practicals', 'Training practicals']
【讨论】:
以上是关于从python中的字典转换为列表的问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
将 pandas.DataFrame 转换为 Python 中的字典列表