我有一个字典列表,像这样:
[{'value': 'apple', 'blah': 2},
{'value': 'banana', 'blah': 3} ,
{'value': 'cars', 'blah': 4}]
我想要['苹果','香蕉','汽车']
做这件事最好的方法是什么?
我有一个字典列表,像这样:
[{'value': 'apple', 'blah': 2},
{'value': 'banana', 'blah': 3} ,
{'value': 'cars', 'blah': 4}]
我想要['苹果','香蕉','汽车']
做这件事最好的方法是什么?
当前回答
[x['value'] for x in list_of_dicts]
其他回答
[x['value'] for x in list_of_dicts]
遵循这个例子——
songs = [
{"title": "happy birthday", "playcount": 4},
{"title": "AC/DC", "playcount": 2},
{"title": "Billie Jean", "playcount": 6},
{"title": "Human Touch", "playcount": 3}
]
print("===========================")
print(f'Songs --> {songs} \n')
title = list(map(lambda x : x['title'], songs))
print(f'Print Title --> {title}')
playcount = list(map(lambda x : x['playcount'], songs))
print(f'Print Playcount --> {playcount}')
print (f'Print Sorted playcount --> {sorted(playcount)}')
# Aliter -
print(sorted(list(map(lambda x: x['playcount'],songs))))
请试试这个。
d =[{'value': 'apple', 'blah': 2}, {'value': 'banana', 'blah': 3} , {'value':
'cars', 'blah': 4}]
b=d[0]['value']
c=d[1]['value']
d=d[2]['value']
new_list=[b,c,d]
print(new_list)
输出:
['apple', 'banana', 'cars']
假设每个字典都有一个值键,你可以这样写(假设你的列表名为l)
[d['value'] for d in l]
如果价值可能丢失,您可以使用
[d['value'] for d in l if 'value' in d]
一个非常简单的方法是:
list1=['']
j=0
for i in com_list:
if j==0:
list1[0]=(i['value'])
else:
list1.append(i['value'])
j+=1
输出:
['apple', 'banana', 'cars']