2013-01-17 8 views
5

Ich bin auf der Suche nach Optimierungen für das folgende Problem (Ich habe ein paar Arbeitscode, aber ich bin mir ziemlich sicher, dass es schneller sein könnte und schlecht geschrieben ist) . Ich habe eine Liste von SKUs (zwischen 6 und 9 Ziffern), über die ich Informationen auf Amazon nachschlage. Der Arbeitscode ist unten angegeben:Der effizienteste Weg, um ein Python-Wörterbuch mit einer Liste zu erstellen

wo x ist ein Wörterbuch von im Allgemeinen wechselnden SKUs und Preisen. Es kommt jedoch zu der Komplikation, dass der Preis nicht gefunden werden kann. In diesem Fall wird die Liste (x) SKU, SKU statt SKU Preis.

Im Moment schaue ich in der Liste der SKUs (in der globalen Variable myList) aber kann nicht helfen, das ist der Zeit Komplexität O (e^n). Angesichts der Tatsache, dass ich mit etwas in der Größenordnung von 20.000 SKUs arbeite, wäre das eher nicht der Fall.

Gibt es eine Möglichkeit, dies weniger komplex zu machen - die gewünschte Ausgabe ist ein Wörterbuch mit jeder SKU einmal (als Schlüssel) und es ist der entsprechende Preis als Wert (ohne Eintrag, wenn es keinen Preis gibt).

edit:

eine Probe des XML Parsen

<?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> 

und myList wie folgt aussieht:

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

die erste Antwort Verwenden von unten ich die folgende Fehlermeldung erhalten:

TypeError: unhashable type: 'list'

wo ich den entsprechenden Code glauben ist:

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 
+0

Können Sie eine kurze Stichprobe von SKUs und Preise beinhalten? –

+2

Auch ein XML-Beispiel, das Sie analysieren, wäre hilfreich. Ich vermute, dass es bessere Möglichkeiten gibt, SKUs und Preise zu extrahieren, die dies vereinfachen könnten. –

+1

'Bereich (len (x))' ist entschieden anti-idiomatisch für Python. Wenn Sie unbedingt numerische Indizes und Elemente benötigen, verwenden Sie 'für idx, Element in enumerate (alist)'. –

Antwort

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

Nur zu sagen, habe das nicht vergessen/aufgehört zu arbeiten. Es läuft gerade jetzt. Wird nach dem Abschluss aktualisiert. –

+0

@ Kali_89 - lass uns die Daumen drücken :) – root

+0

Danke! Es funktionierte, dauert ungefähr 1 Stunde, um die 22.000 SKUs zu tun, die ich als ziemlich gut zähle :) –

Verwandte Themen