2013-01-17 37 views
5

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

Bạn có thể bao gồm một mẫu ngắn của SKU và giá cả? –

+2

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. –

+1

'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)'. –

Trả lời

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

Chỉ cần nói, đã không quên điều này/ngừng làm việc trên đó. Nó chỉ chạy ngay bây giờ. Sẽ cập nhật sau khi hoàn thành. –

+0

@ Kali_89 - hãy để ngón tay của chúng tôi vượt qua :) – root

+0

Cảm ơn! Nó đã làm việc, mất khoảng 1 giờ để làm 22.000 SKU mà tôi đang tính là khá tốt :) –

Các vấn đề liên quan