Poszukuję optymalizacji następującego problemu (mam działający kod, ale jestem dość pewny, że może być szybszy i jest napisany w zły sposób) . Mam listę SKU (od 6 do 9 cyfr), na które szukam informacji w serwisie Amazon. Działający kod jest podany poniżej:Najbardziej efektywny sposób tworzenia słownika Python z listą
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
gdzie x jest słownikiem generalnie zmiennych jednostek SKU i cen. Jednak komplikacja powoduje, że nie można znaleźć ceny. W tym przypadku lista (x) przechodzi do SKU, SKU zamiast do ceny SKU.
W tej chwili szukam na liście SKU (w zmiennej globalnej myList), ale nie mogę pomóc, to złożoność czasu O (e^n). Biorąc pod uwagę, że pracuję z czymś w regionie 20 000 SKU, wolałbym, żeby tak nie było.
Czy istnieje sposób, aby uczynić to mniej skomplikowanym - pożądane wyjście jest słownikiem z każdym SKU raz (jako klucz) i jest to cena odpowiednia jako wartość (bez wpisu, jeśli nie ma ceny).
edit:
próbkę XML przetwarzane
<?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>
i myList wygląda następująco:
myList = ['10032590',
'10043503',
'10047539',
'10055404',
'10058424'...
]
Korzystanie pierwsza odpowiedź poniżej Dostaję następujący komunikat o błędzie:
TypeError: unhashable type: 'list'
gdzie wierzę odpowiedniego kodu jest:
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
można dołączyć krótką próbkę SKU oraz cen? –
Również próbka analizowanego pliku XML byłaby pomocna, podejrzewam, że istnieją lepsze sposoby wyodrębniania jednostek SKU i cen, które mogą ułatwić to. –
'range (len (x))' jest zdecydowanie anty-idiomatyczny dla Pythona. Jeśli bezwzględnie musisz mieć indeksy i elementy numeryczne, użyj 'idx, item in enumerate (alist)'. –