[英]Creating a dictionary of dictionaries in Python
我从查询中获取许多产品的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}
我需要以某种方式将它们及其键和关联值存储起来。 我试图将它们存储在空字典中,如下所示。
product_list = {}
for hit in res['hits']['hits']:
product_list.update(hit["_source"])
print product_list
但这最终仅添加了最后一个字典,因为所有字典的键都相同。 将它们存储在一起的理想方式是什么? 我该如何为此制作字典词典?
您需要使用唯一的标识符(我会使用Id,这就是它的用途)来为字典输入关键字,然后您可以将名称用作内部字典的键,因此外部字典应该将id值链接(如键)到内部字典(作为值),该字典将字段名称(作为键)链接到它们的值。 在所有情况下,这都会产生唯一的密钥。
在这种解决方案中,我猜想字段“ 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
我想为您作为user2923089在他的回答中解释,您需要使用词典列表。
为什么需要使用字典词典? 您是否有某种数据分类? 如果您的所有数据都像:
{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}
就像重复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}
]
也可能您可以随时将所有数据附加到该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)
为什么要列出字典?
如果您拥有不同类型的数据,例如一些具有额外值的数据,或者具有较少值或不同值的数据,则可能是字典的字典,例如:
full_data = {'normal_data':[normal_data_list], 'extra_value': [extra_value_list], 'whatever':whatever_you_need}
因此,您将拥有3或N个不同的词典列表,以防万一。 如果只有N个具有相同数据的字典,则列表将是您的最佳选择,因为您不需要键(如字典中的键)来获取数据列表。 使用代码,如果所有数据都在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.