在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
但是这样做的结果只添加了最后一个字典,因为所有字典的键都是一样的。那么,怎样才能把这些字典一起存储呢?我该如何创建一个字典的字典来解决这个问题呢?
3 个回答
你需要用一个独特的标识符来作为字典的键(我建议用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
我觉得,正如用户2923089在他的回答中解释的,你需要使用一个字典的列表。
为什么要用字典的字典呢?你是不是有某种数据分类?如果你的所有数据都是这样的:
{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