这是我的数据
pricing = [
{
"link": "https://www.kohls.com/product/prd-3751773/laura-geller-iconic-baked-sculpting-lipstick.jsp?skuid=75792684",
"price": "21",
"stock": true,
"title": "Laura Geller Iconic Baked Sculpting Lipstick, Red Overfl",
"seller": "Kohl's"
},
{
"link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
"price": "21.00",
"stock": true,
"title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
"seller": "Macy's"
},
{
"link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
"price": "21.00",
"stock": true,
"title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
"seller": "Wal-Mart.com"
}
]下面是我正在编写的代码,让它们按照列表排序
retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS", "Walgreens","Macy’s", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]
data = sorted(pricing, key=lambda x: retailers.index(x['seller']))错误:
ValueError at /api/product/
"Macy's" is not in list在这里,我试图按照列表对数据进行排序,但是,在对数据进行排序时,我得到了关键错误。请看看我在哪里搞错了
我期待着结果:
pricing = [
{
"link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
"price": "21.00",
"stock": true,
"title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
"seller": "Wal-Mart.com"
},
{
"link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
"price": "21.00",
"stock": true,
"title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
"seller": "Macy's"
},
{
"link": "https://www.kohls.com/product/prd-3751773/laura-geller-iconic-baked-sculpting-lipstick.jsp?skuid=75792684",
"price": "21",
"stock": true,
"title": "Laura Geller Iconic Baked Sculpting Lipstick, Red Overfl",
"seller": "Kohl's"
}
]请看一看
发布于 2020-09-08 05:36:38
你的代码是正确的。问题是"Macy’s"与"Macy's"不一样(注意不同的撇号字符)。
发布于 2020-09-08 05:44:54
您当前的retailers列表如下所示:
retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS", "Walgreens","Macy’s", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]把它改成
retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS", "Walgreens","Macy's", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]我所做的就是把’改为'
顺便说一下,Kohl's看起来是对的
发布于 2020-09-08 06:03:11
在中,代码中有两个错误
macy’s在retailers列表中。macy'strue列表中的而不是 pricing。而不是 True (布尔从python中的大写字母开始)https://stackoverflow.com/questions/63787647
复制相似问题