附加到词典中的列表

时间:2015-07-01 17:36:44

标签: python

目标:将value['itemArray']中的项目附加到Products['Items'] - 请参阅函数fba_orders

问题:当前代码仅将value['itemArray']的最后一项追加到Products['Items']

当前输出:

{'Items': [{'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}, {'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}]}

正确输出将是:

{'Items': [{'SellerFulfillmentOrderItemId': 266804218, 'SellerSKU': 'KM-090914-840-BEARLAPTOP', 'Quantity': 1}, {'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}]}

代码:

import sys

VALUE = {'amountPaid': '43.38',
 'amountSaved': 0.0,
 'buyerCheckoutMessage': '',
 'buyerUserID': 13182254,
 'buyerUserName': 'W5Tiny',
 'checkoutStatus': {'status': 'Complete'},
 'createdTime': '2015-06-30T22:41:01Z',
 'creatingUserRole': 'Buyer',
 'itemArray': [{'item': {'itemID': 266804218,
    'price': '21.1',
    'quantity': 1,
    'sellerInventoryID': 'KM-090914-840-BEARLAPTOP',
    'sk': 'KM-090914-840-BEARLAPTOP',
    'title': u"VTech Bear's Baby Laptop, Blue [Toy]"}},
  {'item': {'itemID': 266804219,
    'price': '22.28',
    'quantity': 1,
    'sellerInventoryID': 'IX-GZ31-31K6',
    'sk': 'IX-GZ31-31K6',
     'title': 'Toy State Caterpillar Push Powered Rev It Up Dump Truck     [Toy]'}}],
 'orderID': 34013525,
 'orderStatus': 'Completed',
 'paidTime': '2015-06-30T22:50:38Z',
 'shippingAddress': {'addressID': 15798541,
  'cityName': 'Nashville',
  'country': 'US',
  'countryName': None,
  'name': 'UNKNOWN',
  'postalCode': '37221',
  'stateOrProvince': 'TN',
  'street1': '123 BOOGIE DRIVE',
  'street2': None},
 'shippingDetails': {'amount': '0.0',
  'insuranceFee': 0,
  'servicesArray': [],
  'shippingService': 'Standard shipping'},
 'subtotal': 43.38,
 'taxAmount': 0.0,
 'total': '43.38',
 'transactionArray': {'transaction': {'buyer': {'email':   'fakewilson259612@hotmail.com'},
   'finalValueFee': '0.0',
   'providerID': '11V84334FD304010L',
   'providerName': 'Paypal'}}}


def fba_order():
    address = {}
    products = {'Items': []}
    item = {}
    Items = []
    address['City'] = VALUE['shippingAddress']['cityName']
    address['CountryCode'] = VALUE['shippingAddress']['country']
    address['Line1'] = VALUE['shippingAddress']['street1']
    address['Line2'] = VALUE['shippingAddress']['street2']
    address['Name'] = VALUE['shippingAddress']['name']
    address['PostalCode'] = VALUE['shippingAddress']['postalCode']
    address['StateOrProvinceCode'] = VALUE['shippingAddress']['stateOrProvince']
    for items in VALUE['itemArray']:
        item['Quantity'] = items['item']['quantity']
        item['SellerFulfillmentOrderItemId'] = items['item']['itemID']
        item['SellerSKU'] = items['item']['sk']
        products['Items'].append(item)
        continue
    print address, '\n', products

if __name__ == '__main__':
    sys.exit(fba_order())

2 个答案:

答案 0 :(得分:3)

您正在重复使用item引用的词典;附加这一个字典不会创建新的副本。相反,您要添加对该字典的多个引用。当您继续更改字典时,所有这些引用都将显示这些更改。

最好为每个循环迭代生成一个全新的字典:

for items in VALUE['itemArray']:
    item = {
        'Quantity': items['item']['quantity'],
        'SellerFulfillmentOrderItemId': items['item']['itemID']
        'SellerSKU': items['item']['sk'],
    }
    products['Items'].append(item)

答案 1 :(得分:3)

问题是你在for循环之外创建项目,然后只是更改for循环中的值并将其附加到列表中。

字典是引用,因此即使在添加products['Items']字典后,如果更改item字典,它也会对附加到列表的item进行更改。

您希望将item初始化为for循环内的新词典。

示例 -

def fba_order():
    address = {}
    products = {'Items': []}
    Items = []
    address['City'] = VALUE['shippingAddress']['cityName']
    address['CountryCode'] = VALUE['shippingAddress']['country']
    address['Line1'] = VALUE['shippingAddress']['street1']
    address['Line2'] = VALUE['shippingAddress']['street2']
    address['Name'] = VALUE['shippingAddress']['name']
    address['PostalCode'] = VALUE['shippingAddress']['postalCode']
    address['StateOrProvinceCode'] = VALUE['shippingAddress']['stateOrProvince']
    for items in VALUE['itemArray']:
        item = {}
        item['Quantity'] = items['item']['quantity']
        item['SellerFulfillmentOrderItemId'] = items['item']['itemID']
        item['SellerSKU'] = items['item']['sk']
        products['Items'].append(item)
        continue
    print address, '\n', products