回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>这是我的数据</p>
<pre><code>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"
}
]
</code></pre>
<p>下面是我正在编写的代码,请按照列表对它们进行排序</p>
<pre><code>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']))
</code></pre>
<p>错误:</p>
<pre><code>ValueError at /api/product/
"Macy's" is not in list
</code></pre>
<p>在这里,我试图根据列表<strong>零售商</strong>按卖家</strong>键对数据进行排序,但是,在对数据排序时,我遇到了键错误。请看一下我哪里出错了</p>
<p>我期待的结果是:</p>
<pre><code>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"
}
]
</code></pre>
<p>请看一看</p>