简体   繁体   English

从选择特定值的字典列表创建字典

[英]Create a dictionary from list of dictionaries selecting specific values

I have a list of dictionaries as below and I'd like to create a dictionary to store specific data from the list.我有一个字典列表如下,我想创建一个字典来存储列表中的特定数据。

test_list = [
    {
        'id':1,
        'colour':'Red',
        'name':'Apple',
        'edible': True,
        'price':100
    },
    {
        'id':2,
        'colour':'Blue',
        'name':'Blueberry',
        'edible': True,
        'price':200
    },
    {
        'id':3,
        'colour':'Yellow',
        'name':'Crayon',
        'edible': False,
        'price':300
    }
]

For instance, a new dictionary to store just the {id, name, price} of the various items.例如,一个新字典只存储各种项目的 {id, name, price}。

I created several lists:我创建了几个列表:

id_list = []
name_list = []
price_list = []

Then I added the data I want to each list:然后我将我想要的数据添加到每个列表中:

for n in test_list:
   id_list.append(n['id']
   name_list.append(n['name']
   price_list.append(n['price']

But I can't figure out how to create a dictionary (or a more appropriate structure?) to store the data in the {id, name, price} format I'd like.但我不知道如何创建字典(或更合适的结构?)以我想要的 {id, name, price} 格式存储数据。 Appreciate help!感谢帮助!

If you don't have too much data, you can use this nested list/dictionary comprehension:如果你没有太多数据,你可以使用这个嵌套列表/字典理解:

keys = ['id', 'name', 'price']
result = {k: [x[k] for x in test_list] for k in keys}

That'll give you:这会给你:


{
  'id': [1, 2, 3],
  'name': ['Apple', 'Blueberry', 'Crayon'],
  'price': [100, 200, 300]
}

I think a list of dictionaries is stille the right data format, so this:我认为字典列表仍然是正确的数据格式,所以:

test_list = [
    {
        'id':1,
        'colour':'Red',
        'name':'Apple',
        'edible': True,
        'price':100
    },
    {
        'id':2,
        'colour':'Blue',
        'name':'Blueberry',
        'edible': True,
        'price':200
    },
    {
        'id':3,
        'colour':'Yellow',
        'name':'Crayon',
        'edible': False,
        'price':300
    }
]

keys = ['id', 'name', 'price']
limited = [{k: v for k, v in d.items() if k in keys} for d in test_list]

print(limited)

Result:结果:

[{'id': 1, 'name': 'Apple', 'price': 100}, {'id': 2, 'name': 'Blueberry', 'price': 200}, {'id': 3, 'name': 'Crayon', 'price': 300}]

This is nice, because you can access its parts like limited[1]['price'] .这很好,因为您可以访问它的部分,例如limited[1]['price']

However, your use case is perfect for pandas , if you don't mind using a third party library:但是,如果您不介意使用第三方库,您的用例非常适合pandas

import pandas as pd

test_list = [
    {
        'id':1,
        'colour':'Red',
        'name':'Apple',
        'edible': True,
        'price':100
    },
    {
        'id':2,
        'colour':'Blue',
        'name':'Blueberry',
        'edible': True,
        'price':200
    },
    {
        'id':3,
        'colour':'Yellow',
        'name':'Crayon',
        'edible': False,
        'price':300
    }
]

df = pd.DataFrame(test_list)

print(df['price'][1])
print(df)

The DataFrame is perfect for this stuff and selecting just the columns you need: DataFrame 非常适合这些东西,只需选择您需要的列:

keys = ['id', 'name', 'price']
df_limited = df[keys]
print(df_limited)

The reason I'd prefer either to a dictionary of lists is that manipulating the dictionary of lists will get complicated and error prone and accessing a single record means accessing three separate lists - there's not a lot of advantages to that approach except maybe that some operations on lists will be faster, if you access a single attribute more often.我更喜欢列表字典的原因是,操作列表字典会变得复杂且容易出错,并且访问单个记录意味着访问三个单独的列表 - 除了某些操作之外,这种方法没有很多优点如果您更频繁地访问单个属性,on list 会更快。 But in that case, pandas wins handily.但在这种情况下, pandas轻松获胜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM