Tôi đang tìm kiếm tối ưu hóa về vấn đề sau (Tôi có một số mã làm việc nhưng tôi khá chắc chắn nó có thể nhanh hơn và được viết theo cách xấu) . Tôi đã có một danh sách các SKU (từ 6 đến 9 chữ số) mà tôi đang tìm kiếm thông tin trên Amazon. Mã làm việc được cung cấp bên dưới:Cách hiệu quả nhất để tạo một từ điển Python được đưa ra một danh sách
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
trong đó x là từ điển của các SKU và giá cả thay thế. Tuy nhiên, các biến chứng phát sinh theo đó giá không thể được tìm thấy. Trong trường hợp này, danh sách (x) chuyển sang SKU, SKU thay vì giá SKU.
Hiện tại tôi đang tra cứu danh sách SKU (trong biến toàn cầu myList) nhưng không thể giúp điều này phức tạp về thời gian O (e^n). Vì tôi đang tìm cách làm việc với một thứ gì đó trong khu vực 20.000 SKU, tôi không thích điều này.
Có cách nào để làm việc này ít phức tạp hơn - đầu ra mong muốn là từ điển với mỗi SKU một lần (dưới dạng khóa) và giá tương ứng với giá trị đó (không có mục nhập nếu không có giá).
chỉnh sửa:
một mẫu XML được phân tích cú pháp
<?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>
và myList trông giống như:
myList = ['10032590',
'10043503',
'10047539',
'10055404',
'10058424'...
]
Sử dụng câu trả lời đầu tiên dưới đây tôi nhận được thông báo lỗi sau:
TypeError: unhashable type: 'list'
nơi tôi tin rằng các mã có liên quan là:
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
Bạn có thể bao gồm một mẫu ngắn của SKU và giá cả? –
Ngoài ra, một mẫu XML bạn đang phân tích cú pháp sẽ hữu ích, tôi nghi ngờ rằng có nhiều cách tốt hơn để trích xuất SKU và giá có thể giúp đơn giản hơn. –
'phạm vi (len (x))' là decidedly chống idiomatic cho python. Nếu bạn hoàn toàn phải có các chỉ mục và các mục số, hãy sử dụng 'cho idx, mục trong liệt kê (alist)'. –