给定一个列表,生成 Python 字典的最有效方法是什么?

5

我正在寻求以下问题的优化(我有一些可工作的代码,但我相当肯定它可以更快速且编写方式不佳)。我有一个SKU列表(介于6到9位数字之间),我正在亚马逊上查找相关信息。下面是可工作代码:

def buildDictionary2(stringy):
    x = stringy.xpath('//sellersku/text()|//product/descendant::amount[1]/text()')
    Sku_To_Price = {}
    for items in range(len(x)):
        if x[items] in myList:
            try:
                if x[items+1] not in myList:
                    Sku_To_Price[x[items]] = x[items+1]
                else:
                    Sku_To_Price[x[items]] = ''
            except:
                pass
        else:
            pass
    return Sku_To_Price

其中x是一个通常交替的SKU和价格字典。但是,问题在于找不到价格。在这种情况下,列表(x)变成了SKU、SKU,而不是SKU价格。

目前,我正在查找SKU列表(全局变量myList),但无法避免时间复杂度为O(e^n)的情况。考虑到我要处理大约20,000个SKU,我宁愿不要这样。

有没有办法使这个过程变得简单一些——期望的输出是一个字典,每个SKU只出现一次(作为键),其对应的价格作为值(如果没有价格,则没有条目)。

编辑:

被解析的XML样本

<?xml version="1.0" ?>
<GetLowestOfferListingsForSKUResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
  <GetLowestOfferListingsForSKUResult SellerSKU="X" status="Success">
    <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
    <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" 
             xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
      <Identifiers>
        <MarketplaceASIN>
          <MarketplaceId>X</MarketplaceId>
          <ASIN>X</ASIN>
        </MarketplaceASIN>
        <SKUIdentifier>
          <MarketplaceId>X</MarketplaceId>
          <SellerId>X</SellerId>
          <SellerSKU>10065897</SellerSKU>
        </SKUIdentifier>
      </Identifiers>
      <LowestOfferListings>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>New</ItemCondition>
            <ItemSubcondition>New</ItemSubcondition>
            <FulfillmentChannel>Amazon</FulfillmentChannel>
            <ShipsDomestically>True</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>3</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>23.68</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>Merchant</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
      </LowestOfferListings>
    </Product>
  </GetLowestOfferListingsForSKUResult>
  <GetLowestOfferListingsForSKUResult SellerSKU="X" status="X">
    <AllOfferListingsConsidered>X</AllOfferListingsConsidered>
    <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
             xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
      <Identifiers>
        <MarketplaceASIN>
          <MarketplaceId>X</MarketplaceId>
          <ASIN>X</ASIN>
        </MarketplaceASIN>
        <SKUIdentifier>
          <MarketplaceId>X</MarketplaceId>
          <SellerId>X</SellerId>
          <SellerSKU>9854521</SellerSKU>
        </SKUIdentifier>
      </Identifiers>
      <LowestOfferListings>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>2.68</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>8</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>Merchant</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>4</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
      </LowestOfferListings>
    </Product>
  </GetLowestOfferListingsForSKUResult>
  <ResponseMetadata>
    <RequestId>X</RequestId>
  </ResponseMetadata>
</GetLowestOfferListingsForSKUResponse>

我的myList看起来像:

myList = ['10032590',
'10043503',
'10047539',
'10055404',
'10058424'...
]

使用下面的第一个答案时,我遇到了以下错误信息:

TypeError: unhashable type: 'list'

我认为相关代码如下:

def xml_to_dict(self, xml):
    doc = lh.fromstring(xml)
    d = {}
    for product in doc.xpath('.//product'):
        sku = product.xpath('.//sellersku/text()')
        amount = product.xpath('./descendant::amount[1]/text()')
        d[sku] = amount
    return d

你能否包含一些SKU和价格的简短样例? - Martijn Pieters
2
此外,提供你正在解析的 XML 的示例将会很有帮助。我怀疑有更好的方法来提取 SKU 和价格,这可以使得处理变得更简单。 - Martijn Pieters
1
range(len(x)) 对于 Python 来说显然是反常的。如果你一定要使用数字索引和元素,请使用 for idx, item in enumerate(alist) - g.d.d.c
@MartijnPieters 按照要求添加了XML和myList :) - Kali_89
1
@RandyE你好,Randy,我想这个问题是了解亚马逊市场API(Amazon MWS)的第一步。其中一个工具可以让你运行一个定价比较报告,比较你已经列出的SKU和当前最低价格。当你只有一个或两个SKU时,手动检查它们是可以的。但是,随着SKU数量的增加和你想要检查它们的频率的增加,效率变得更加重要 - 因此有了这个问题! - Kali_89
显示剩余3条评论
1个回答

6
d={}
for product in doc.xpath('.//product'):
    sku = product.xpath('.//sellersku/text()')[0]
    price = product.xpath('./descendant::amount[1]/text()')
    if price: # if theres a possibility of sku missing replace with:
              # "if price and sku"
              #
              # if you have duplicate sku's and you don't want them overwritten 
              # add "and sku not in d" check
        d[sku]= price[0]

2
只是想说,我没有忘记这个项目或停止工作。它正在运行中。完成后我会更新进展。 - Kali_89
@Kali_89 -- 让我们祈求好运 :) - root
谢谢!它起作用了,大约需要1小时来处理22,000个SKU,我认为这相当不错 :) - Kali_89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接