Here is my data
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"
}
]
Below code i am writing make them sort as per the list
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']))
error:
ValueError at /api/product/
"Macy's" is not in list
Here i am trying to sort data by seller key as per the list retailers but, i am getting key error while sorting the data. Please have a look where i am making mistake
I am expecting result:
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"
}
]
Please have a look
Your code is correct. The problem is "Macy's"
is not the same as "Macy's"
(note the different apostrophe character).
Your current retailers
list shows this way:
retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS", "Walgreens","Macy’s", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]
Change it to
retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS", "Walgreens","Macy's", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]
All I did was to change '
to '
Btw, Kohl's
looks correct
There where 2 errors in your code
macy's
in retailers
list. instead of macy's
true
in pricing
list. instead of True
(Boolean starts with capital Letter in python)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.