python - 从列表中删除词典

标签 python

我有以下列表:

items = [{'item': 1}, {'item': 2}, {'item': 3}]

如何将其转换为这个?

items = [1, 2, 3]

这是我当前的解决方案,但我想知道是否有更好的解决方案

items = [item['item'] for item in items]

最佳答案

不确定这是否比您的解决方案更好,但您可以使用 itemgetter

import operator
map(operator.itemgetter('item'), items)

在 python 3.x 中,您需要将结果转换为 list

关于python - 从列表中删除词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201167/

相关文章:

python - 属性错误 : 'int' object has no attribute 'lower' in TFIDF and CountVectorizer

python - Beautifulsoup,找到htm中唯一没有属性的标签

python - Django 南 : Migrating FK from another table

python - 给定一个 "jumbled"列表 L,得到一个列表,其中每个元素都是 L 对应元素的索引,如果 L 已排序

python - 带有groupby的 Pandas 数据框滚动窗口

python - 是否可以让这个 Xpath 变得更短?

python - 传递一个变量以从 Python 中的 JSON 字符串中提取?

python - selenium.common.exceptions.ElementNotVisibleException : Message: Element is not currently visible and so may not be interacted with

python - 无需算术运算符即可计算 x/y

python - 在 Tkinter 中为一组小部件添加滚动条