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'...
]

使用下面的第一个答案,我收到以下错误消息:

类型错误:不可散列类型:“列表”

我认为相关代码是:

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
4

1 回答 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]
于 2013-01-17T17:26:04.670 回答