使用不同词典中的不同字段创建字典

时间:2014-06-04 11:25:16

标签: python json dictionary

我有这样的清单:

two_dimension_sizelist = \
[{u'sizeOptionId': u'1542',
  u'sizeOptionName': u'1',
  u'sortOrderNumber': u'915'},
 {u'sizeOptionId': u'1543',
  u'sizeOptionName': u'2',
  u'sortOrderNumber': u'975'},
  ...
 {u'sizeOptionId': u'1602',
  u'sizeOptionName': u'Long',
  u'sortOrderNumber': u'6873'}]

我有一些产品:

{
"businessCatalogItemId":"5453220021802",
"inventoryStatusId":"0",
"colorName":"Medium Wash",
"sizeVariantId":"1",
"upcCode":"197476818021",
"onOrderDate":"2014-07-04T00:00:00-04:00",
"currentMinPrice":"34.94",
"currentPrice":"0.0",
"baseColorId":"1021",
"isClearanceItem":"false",
"catalogItemTypeId":"3",
"sizeDimension1Id":"1591",
"catalogItemSubtypeId":"15",
"isInStock":"true",
"skuId":"5453220021802",
"regularMaxPrice":"34.94",
"nowPrice":null,
"variantName":"Regular",
"isOnOrder":"false",
"mailOnlyReturn":"M",
"regularMinPrice":"34.94",
"reservable":"true",
"onlyAvailableOnline":"false",
"catalogItemId":"5146840",
"regularPrice":"0.0",
"isLowInventory":"false",
"sizeDimension2Id":"1601",
"priceType":"1",
"currentMaxPrice":"34.94"
},
{
"businessCatalogItemId":"5453220021803",
"inventoryStatusId":"4",
"colorName":"Medium Wash",
"sizeVariantId":"1",
"upcCode":"197476818038",
"onOrderDate":"2014-07-02T00:00:00-04:00",
"currentMinPrice":"34.94",
"currentPrice":"0.0",
"baseColorId":"1021",
"isClearanceItem":"false",
"catalogItemTypeId":"3",
"sizeDimension1Id":"1591",
"catalogItemSubtypeId":"15",
"isInStock":"true",
"skuId":"5453220021803",
"regularMaxPrice":"34.94",
"nowPrice":null,
"variantName":"Regular",
"isOnOrder":"true",
"mailOnlyReturn":"M",
"regularMinPrice":"34.94",
"reservable":"true",
"onlyAvailableOnline":"true",
"catalogItemId":"5146832",
"regularPrice":"0.0",
"isLowInventory":"false",
"sizeDimension2Id":"1602",
"priceType":"1",
"currentMaxPrice":"34.94"
}

每个产品都可以有sizeDimension1Id和sizeDimension2Id,或者只有sizeDimension1Id或者none,我应该为每个产品映射每个大小的two_diminsion_sizelist来构建dict:

{'size2Name': 'Long', 'size2Id': '1602', 'size1Name': '16', 'size1Id': '1590', {other product parameters}}

我做了这件事:

for dict_size in two_dimension_sizelist:
    two_dimension_sizedict.update(
        {dict_size['sizeOptionId']: dict_size['sizeOptionName']})

这给了我two_dimension_sizedict:

{u'1542': u'1',
 u'1543': u'2',
 u'1590': u'16',
 u'1591': u'18',
 u'1601': u'Regular',
 u'1602': u'Long',
 u'1604': u'Short',
 u'1640': u'4',
 u'1642': u'6',
 u'1644': u'8',
 u'1645': u'10',
 u'1646': u'12',
 u'1647': u'14'}

我做对了吗? 现在我不知道如何将这种尺寸与产品结合起来。

1 个答案:

答案 0 :(得分:0)

在我看来,您会多次扫描two_dimension_sizelist并从您的产品词典中搜索匹配的“sizeOptionId”。

我建议这样做:

all_size_list = dict((str(x['sizeOptionId']), str(x['sizeOptionName'])) for x in two_dimension_sizelist)

for product in products_list:
    if "sizeDimension1Id" in product.keys():
        size = product['sizeDimension1Id']
        if size in all_size_list.keys():
            product.update({
                'size1Name': all_size_list[size],
                'size1Id': size
            })

        # This can exist only if size1 exists
        if "sizeDimension2Id" in product.keys():
            size = product['sizeDimension2Id']
            if size in all_size_list.keys():
                product.update({
                    'size2Name': all_size_list[size],
                    'size2Id': size
                })

您的产品列表似乎是JSON字符串,请使用json库转换Python值。