gpt4 book ai didi

python - 给定列表生成 Python 字典的最有效方法

转载 作者:太空狗 更新时间:2023-10-30 01:02:40 27 4
gpt4 key购买 nike

我正在寻找对以下问题的优化(我有一些工作代码,但我相当确定它可以更快并且以糟糕的方式编写)。我有一个 SKU 列表(6 到 9 位数字),我正在亚马逊上查找这些信息。工作代码如下:

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

其中 x 是通常交替的 SKU 和价格的字典。但是,出现了无法找到价格的复杂情况。在这种情况下,列表 (x) 是 SKU,SKU 而不是 SKU 价格。

目前我正在查找 SKU 列表(在全局变量 myList 中),但无法帮助这是时间复杂度 O(e^n)。考虑到我正在考虑使用 20,000 个 SKU 范围内的产品,我希望情况并非如此。

有没有办法让它变得不那么复杂 - 所需的输出是一个字典,每个 SKU 一次(作为键),它对应的价格作为值(如果没有价格,则没有条目)。

编辑:

正在解析的 XML 示例

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

myList 看起来像:

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

使用下面的第一个答案,我收到以下错误消息:

TypeError: unhashable type: 'list'

我认为相关代码是:

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

最佳答案

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]

关于python - 给定列表生成 Python 字典的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14384450/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com