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
Können Sie eine kurze Stichprobe von SKUs und Preise beinhalten? –
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. –
'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)'. –