我有像
这样的数据data = [{'name':'a', 'description':'1'}, {'name':'b', 'description':'2'}]
我想将上面的内容转换为这样的列表:
data = [['a', '1'], ['b', '2']]
请告诉我怎么做?
答案 0 :(得分:1)
您可以使用operator.itemgetter
,就像这样
from operator import itemgetter
getter = itemgetter("name", "description")
print([getter(item) for item in data])
# [('a', '1'), ('b', '2')]
如果您想要列表,那么您可以
print([list(getter(item)) for item in data])
# [['a', '1'], ['b', '2']]
答案 1 :(得分:0)
data = [[entry['name'], entry['description']] for entry in data]
答案 2 :(得分:0)
data = [{'name':'a', 'description':'1'}, {'name':'b', 'description':'2'}]
c = []
for d in data:
c.append(d.values())
print c
Output: [['a', '1'], ['b', '2']]
答案 3 :(得分:0)
列表列表的词典列表:
lstoflst=[list(lstofdct[0].keys()) ] + [ list(x.values()) for x in lstofdct] )
词典列表列表:
lstofdct=[ dict(zip(lstoflst[0],x)) for x in lstoflst[1:len(lstoflst)] ]