在Python中创建字典字典

时间:2014-06-05 08:12:33

标签: 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 个答案:

答案 0 :(得分:0)

你需要使用一个唯一的标识符键入你的字典(我使用了Id,它的用途是什么),然后你可以使用这些名字作为内部字典的键,这样你的外表dictionary应该将id值(作为键)链接到内部字典(作为值),该字典将vield的名称(作为键)链接到它们的值。这会在所有情况下产生唯一的密钥。

答案 1 :(得分:0)

在那个解决方案中我想那个字段' 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

答案 2 :(得分:0)

我认为你作为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