简体   繁体   English

在Python中创建字典词典

[英]Creating a dictionary of dictionaries in Python

I'm getting a number of python dictionaries of products from a query as follows 我从查询中获取许多产品的python字典,如下所示

{u'rating': 0.0, u'name': u'Samsung NP550P5C-S02IN (3rd Gen Ci7/ 8GB/ 1TB/ Win7 HP/ 2GB Graphics)', u'price': 0.0, u'popularity': 9, u'brand_id': 1, u'category_id': 3, u'id': 3794}

{u'rating': 10.0, u'name': u'Samsung NP300E5A-A08IN Laptop (2nd Gen Pentium Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 23, u'brand_id': 1, u'category_id': 3, u'id': 3825}

{u'rating': 0.0, u'name': u'Samsung NP-RV515-A02IN Laptop (APU Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 26, u'brand_id': 1, u'category_id': 3, u'id': 3826}

{u'rating': 9.2, u'name': u'Samsung NP355V5C-S03IN Laptop (APU Quad Core A8/ 6GB/ 750GB/ Win7 HP/ 1GB Graphics)', u'price': 0.0, u'popularity': 17, u'brand_id': 1, u'category_id': 3, u'id': 3889}

I need to store them somehow with their key and associated values. 我需要以某种方式将它们及其键和关联值存储起来。 I tried to store them in an empty dictionary as follows. 我试图将它们存储在空字典中,如下所示。

product_list = {}
for hit in res['hits']['hits']:
    product_list.update(hit["_source"])
print product_list

But this ended up adding only the last dictionary because the keys of all the dictionary are the same. 但这最终仅添加了最后一个字典,因为所有字典的键都相同。 What would be the ideal way to store these together? 将它们存储在一起的理想方式是什么? How can I make a dictionary of dictionaries for this? 我该如何为此制作字典词典?

You need to key your dictionary using a unique identifier (I'd use the Id, that's what its there for) and then you can use the names as the keys on the inner dictionaries so your outer dictionary should link the id value (as the key) to an inner dictionary (as the value) which links the names of the vields (as the key) to their values. 您需要使用唯一的标识符(我会使用Id,这就是它的用途)来为字典输入关键字,然后您可以将名称用作内部字典的键,因此外部字典应该将id值链接(如键)到内部字典(作为值),该字典将字段名称(作为键)链接到它们的值。 This results in a unique key in all cases. 在所有情况下,这都会产生唯一的密钥。

In that solution i guess that field 'id' is unique. 在这种解决方案中,我猜想字段“ id”是唯一的。

a = {u'rating': 0.0, u'name': u'Samsung NP550P5C-S02IN (3rd Gen Ci7/ 8GB/ 1TB/ Win7 HP/ 2GB Graphics)', u'price': 0.0, u'popularity': 9, u'brand_id': 1, u'category_id': 3, u'id': 3794}
b = {u'rating': 10.0, u'name': u'Samsung NP300E5A-A08IN Laptop (2nd Gen Pentium Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 23, u'brand_id': 1, u'category_id': 3, u'id': 3825}
c = {u'rating': 0.0, u'name': u'Samsung NP-RV515-A02IN Laptop (APU Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 26, u'brand_id': 1, u'category_id': 3, u'id': 3826}
d = {u'rating': 9.2, u'name': u'Samsung NP355V5C-S03IN Laptop (APU Quad Core A8/ 6GB/ 750GB/ Win7 HP/ 1GB Graphics)', u'price': 0.0, u'popularity': 17, u'brand_id': 1, u'category_id': 3, u'id': 3889}

product_list = {}

for i in [a,b,c,d]:
    product_list[i.pop('id')] = i

I think for you as user2923089 explain in his answer you need to use a list of dictionaries. 我想为您作为user2923089在他的回答中解释,您需要使用词典列表。

Why do you need to use a dictionary of dictionary ? 为什么需要使用字典词典? Do hou have some kind of data classification ? 您是否有某种数据分类? If all your data is like: 如果您的所有数据都像:

{u'rating': 0.0, u'name': u'Samsung NP550P5C-S02IN (3rd Gen Ci7/ 8GB/ 1TB/ Win7 HP/ 2GB Graphics)', u'price': 0.0, u'popularity': 9, u'brand_id': 1, u'category_id': 3, u'id': 3794}

Just like that repeated x times, you don't need to use a dictonary of dictionaries, you need to use a list of dictionaries like : 就像重复x次一样,您不需要使用字典的字典,您需要使用字典列表,例如:

full_list = [
{u'rating': 0.0, u'name': u'Samsung NP550P5C-S02IN (3rd Gen Ci7/ 8GB/ 1TB/ Win7 HP/ 2GB Graphics)', u'price': 0.0, u'popularity': 9, u'brand_id': 1, u'category_id': 3, u'id': 3794}, 
{u'rating': 10.0, u'name': u'Samsung NP300E5A-A08IN Laptop (2nd Gen Pentium Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 23, u'brand_id': 1, u'category_id': 3, u'id': 3825}, 
{u'rating': 0.0, u'name': u'Samsung NP-RV515-A02IN Laptop (APU Dual Core/ 2GB/ 500GB/ Win7 HB)', u'price': 0.0, u'popularity': 26, u'brand_id': 1, u'category_id': 3, u'id': 3826}, 
{u'rating': 9.2, u'name': u'Samsung NP355V5C-S03IN Laptop (APU Quad Core A8/ 6GB/ 750GB/ Win7 HP/ 1GB Graphics)', u'price': 0.0, u'popularity': 17, u'brand_id': 1, u'category_id': 3, u'id': 3889}
]

also maybe you could append to that full_list any data at any time like: 也可能您可以随时将所有数据附加到该full_list ,例如:

new_data = {u'rating': 0.0, u'name': u'Samsung NP550P5C-S02IN (3rd Gen Ci7/ 8GB/ 1TB/ Win7 HP/ 2GB Graphics)', u'price': 0.0, u'popularity': 9, u'brand_id': 1, u'category_id': 3, u'id': 3794}
full_list.append(new_data)

Why list of dictionaries ? 为什么要列出字典?

If you have different kind of data, like some data with extra values, or with less values or different values, maybe a dictionary of dictionaries like: 如果您拥有不同类型的数据,例如一些具有额外值的数据,或者具有较少值或不同值的数据,则可能是字典的字典,例如:

full_data = {'normal_data':[normal_data_list], 'extra_value': [extra_value_list], 'whatever':whatever_you_need}

So you will have 3 or N different list of dictionaries, just in case you need it. 因此,您将拥有3或N个不同的词典列表,以防万一。 If you only have N dictionaries with same data, a list will be the best choice for you, because you don't need a key (like in a dictionary) to get the list of data. 如果只有N个具有相同数据的字典,则列表将是您的最佳选择,因为您不需要键(如字典中的键)来获取数据列表。 Using your code, if you have all the data in res['hits']['hits'] you can create the full data list like: 使用代码,如果所有数据都在res ['hits'] ['hits']中,则可以创建完整的数据列表,如下所示:

product_list = []
for hit in res['hits']['hits']:
    product_list.append(hit["_source"])
print product_list

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

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