简体   繁体   English

如何在列表中添加命令数据

[英]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.

相关问题 如何根据两个列表字典中特定键的comman值从两个字典列表中获取comman数据? - How to get comman data from two list of dictionaries based on having comman values for a specific key in both list's dictionaries? 如何在下面的列表中添加数据? - How to add data in list below? 如何在peewee中添加连接数据列表? - How to add a list of joined data in peewee? 如何将列表列表添加到现有数据框中作为单独的列 - How to add a List of list to an existing data frame as separate columns 如何从列表中拆分子列表并将数据添加到另一个列表中 - How to split the sublist from the list and add the data in another list 如何将输入的数据添加到列表中而不将每个单词显示为数据 - How to add inputted data into a list without displaying each word as data 如何将列表添加到列表列表 - How to add a list to a list of list 如何在 django 列表中添加按钮以将列表的数据添加到 HTML 文本字段? - How to add a button in a django list to add List's data to a HTML TextField? 如何根据姓名、地址识别人的关系,然后通过 linux comman 或 Pyspark 分配相同的 ID - how to identify people's relationship based on name, address and then assign a same ID through linux comman or Pyspark 如何创建一个大小为500的元组的分块列表,然后向其中添加数据? - How to create a chunked list of tuples of size 500 and then add data to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM