如何删除 JSON 对象列表中给定值的重复项?

我正在解析以下数据:

master = [
 {'Title': 'Jordan MA2 "Future Beginnings"','Price': 150,'Currency': 'USD','Picture': 'https://static.nike.com/a/images/t_default/a960ad50-4a05-4066-9916-e334e68f1dfd/jordan-ma2-future-beginnings-shoes-bK3TsG.png','Link': 'nike.com/t/jordan-ma2-future-beginnings-shoes-bK3TsG/DA2552-100','Brand': 'nike'},{'Title': 'Jordan MA2 "Future Beginnings"','Brand': 'jordan'},{'Title': 'lace-up leather boots','Price': 1904,'Currency': ' USD','Picture': 'https://cdn-images.farfetch-contents.com/16/49/73/72/16497372_32445982_480.jpg','Link': 'farfetch.com/shopping/men/rick-owens-lace-up-leather-boots-item-16497372.aspx?storeid=11893','Brand': 'yeezy'},{'Title': 'Air Jordan XXXV Low','Price': 175,'Picture': 'https://static.nike.com/a/images/t_default/a4778a7d-3678-4f7f-a289-773b1e9faf01/air-jordan-xxxv-low-basketball-shoes-6q5Z2t.png','Link': 'nike.com/t/air-jordan-xxxv-low-basketball-shoes-6q5Z2t/DJ9805-190',{'Title': 'The Lug slip-on boots','Price': 1250,'Picture': 'https://cdn-images.farfetch-contents.com/17/08/52/70/17085270_34331632_480.jpg','Link': 'farfetch.com/shopping/men/bottega-veneta-the-lug-slip-on-boots-item-17085270.aspx?storeid=9671',{'Title': 'Jordan Series .01','Price': 80,'Picture': 'https://static.nike.com/a/images/t_default/2527995d-01cd-43ea-b66d-0f1248035bb3/jordan-series-1-shoes-TxmFhR.png','Link': 'nike.com/t/jordan-series-1-shoes-TxmFhR/CV8129-100','Brand': 'nike'}
]

列表中的前两个json对象有相同的图片链接,但品牌不同。如何删除具有相同图片链接和相同标题的两者之一?

我已经试过了,但它似乎不会改变数据。

master = { each['Picture'] : each for each in master }.values()

问候

snowcarangid 回答:如何删除 JSON 对象列表中给定值的重复项?

您可以使用 set 检查是否存在重复。例如:

out,seen = [],set()
for d in master:
    if (d["Title"],d["Picture"]) not in seen:
        seen.add((d["Title"],d["Picture"]))
        out.append(d)

print(out)

打印:

[
    {
        "Title": 'Jordan MA2 "Future Beginnings"',"Price": 150,"Currency": "USD","Picture": "https://static.nike.com/a/images/t_default/a960ad50-4a05-4066-9916-e334e68f1dfd/jordan-ma2-future-beginnings-shoes-bK3TsG.png","Link": "nike.com/t/jordan-ma2-future-beginnings-shoes-bK3TsG/DA2552-100","Brand": "nike",},{
        "Title": "lace-up leather boots","Price": 1904,"Currency": " USD","Picture": "https://cdn-images.farfetch-contents.com/16/49/73/72/16497372_32445982_480.jpg","Link": "farfetch.com/shopping/men/rick-owens-lace-up-leather-boots-item-16497372.aspx?storeid=11893","Brand": "yeezy",{
        "Title": "Air Jordan XXXV Low","Price": 175,"Picture": "https://static.nike.com/a/images/t_default/a4778a7d-3678-4f7f-a289-773b1e9faf01/air-jordan-xxxv-low-basketball-shoes-6q5Z2t.png","Link": "nike.com/t/air-jordan-xxxv-low-basketball-shoes-6q5Z2t/DJ9805-190",{
        "Title": "The Lug slip-on boots","Price": 1250,"Picture": "https://cdn-images.farfetch-contents.com/17/08/52/70/17085270_34331632_480.jpg","Link": "farfetch.com/shopping/men/bottega-veneta-the-lug-slip-on-boots-item-17085270.aspx?storeid=9671",{
        "Title": "Jordan Series .01","Price": 80,"Picture": "https://static.nike.com/a/images/t_default/2527995d-01cd-43ea-b66d-0f1248035bb3/jordan-series-1-shoes-TxmFhR.png","Link": "nike.com/t/jordan-series-1-shoes-TxmFhR/CV8129-100",]
,

您非常接近正确的解决方案,只需创建组合键 x['Title']+x['Picture'] + x['Link'],然后取值:

list({x['Title']+x['Picture'] + x['Link']:x for x in master}.values())
本文链接:https://www.f2er.com/823.html

大家都在问