从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 中的字典列表

将列表列表转换为Python中的字典字典

Python:如何从列表中的字典创建 DataFrame 列 [重复]

python字典转换为list后选中最后一个值

Python-将隐藏在列表中的字典转换为数据帧

Python。将 2 个列表转换为一个字典对象 [重复]