[英]How to add comman data in a list of
I have a list of dicts which has a lot of values like我有一个字典列表,其中包含很多值,例如
"resultset":[
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"10",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"11",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"10",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"14",
},
{
"Car":"Lambo ",
"Value_in_US":"11",
},
{
"Car":"Porshe Nine",
"Value_in_US":"12",
},
{
"Car":"Porshe Nine",
"Value_in_US":"10",
},
]
I am trying to separate the list of unique Cars with corresponding values, The output i am expecting is我正在尝试将具有相应值的唯一汽车列表分开,我期望的 output 是
{
'Porshe Nine' : [10,12],
'Lambo': [11],
'Aston Martin DB Nine': [10,11,10,14]
}
I was trying something like我正在尝试类似的东西
test = {}
for data in result['resultset']:
test[data['Car']] = ???
I am not sure how to get the values right as i mentioned我不确定如何正确设置我提到的值
Sounds like a defaultdict
would be useful here:听起来
defaultdict
在这里很有用:
from collections import defaultdict
test = defaultdict(list)
for data in result['resultset']:
test[data['Car']].append(data['Value_in_US'])
Very similar logic to the code you already have, but what defaultdict
lets you do is assume that there's already a key/value pair of car:list of prices.与您已经拥有的代码非常相似的逻辑,但是
defaultdict
让您做的是假设已经有一个键/值对 car:list of prices。 Notice that we don't need to check if the car is already in the dictionary, we just append the price.请注意,我们不需要检查汽车是否已经在字典中,我们只需 append 价格。
Without any additional imports:没有任何额外的进口:
test = {}
for data in result['resultset']:
if not data['Car'] in test.keys():
test[data['Car']] = []
test[data['Car']].append(data['Value_in_US'])
works作品
You can use itertools.groupby
to do this easily您可以使用
itertools.groupby
轻松完成此操作
resultset = [
{'Car': 'Aston Martin DB Nine', 'Value_in_US': '10'},
{'Car': 'Aston Martin DB Nine', 'Value_in_US': '11'},
{'Car': 'Aston Martin DB Nine', 'Value_in_US': '10'},
{'Car': 'Aston Martin DB Nine', 'Value_in_US': '14'},
{'Car': 'Lambo ', 'Value_in_US': '11'},
{'Car': 'Porshe Nine', 'Value_in_US': '12'},
{'Car': 'Porshe Nine', 'Value_in_US': '10'}]
from itertools import groupby
f = lambda d: d['Car']
res = {k:[d2['Value_in_US'] for d2 in g] for k,g in groupby(sorted(resultset, key=f), f)}
print(res)
Outout输出
{'Aston Martin DB Nine': ['10', '11', '10', '14'],
'Lambo ': ['11'],
'Porshe Nine': ['12', '10']}
Dict/list comprehension should do the trick if you like inline expressions,如果您喜欢内联表达式,字典/列表理解应该可以解决问题,
resultset=[
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"10",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"11",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"10",
},
{
"Car":"Aston Martin DB Nine",
"Value_in_US":"14",
},
{
"Car":"Lambo ",
"Value_in_US":"11",
},
{
"Car":"Porshe Nine",
"Value_in_US":"12",
},
{
"Car":"Porshe Nine",
"Value_in_US":"10",
},
]
#init with empty list for each cars
result = {dic['Car']:[] for dic in resultset}
#append the value to each car entry
[result[dic['Car']].append(dic['Value_in_US']) for dic in resultset]
print(result)
>>>> {'Aston Martin DB Nine': ['10', '11', '10', '14'], 'Lambo ': ['11'], 'Porshe Nine': ['12', '10']}
Here is a good guide on medium to start with dictionary comprehension.这是一个很好的媒体指南,从字典理解开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.