根据列表字典中的关键字更新重复字段

时间:2019-05-09 22:12:51

标签: python dictionary hashmap duplicates

提供名单

l = [
    {
        "URL": "https://www.nytimes.com/",
        "ACTION": "FOLLOW",
        "RESULTS": "/PAGES/222437976487981212229928695878437391142.png",
        "PAGE-UUID": 2.224379764879812e+38,
        "PARENT-UUID": 2.224379764879812e+38
    },
    {
        "URL": "https://www.nytimes.com/es/",
        "ACTION": "NEW",
        "RESULTS": "/PAGES/138695820074592921946528124297895673746.png",
        "PAGE-UUID": 138695820074592921946528124297895673746,
        "PARENT-UUID": 2.224379764879812e+38
    },
    {
        "URL": "https://www.nytimes.com/",
        "ACTION": "NEW",
        "RESULTS": "/PAGES/166947399632926520629187111715759306140.png",
        "PAGE-UUID": 166947399632926520629187111715759306140,
        "PARENT-UUID": 2.436661515947743e+38
    },
    {
        "URL": "https://www.nytimes.com/subscriptions/Multiproduct/lp3L3W6.html?campaignId=6W74R",
        "ACTION": "NEW",
        "RESULTS": "/PAGES/299203350572384506529421004856026300297.png",
        "PAGE-UUID": 299203350572384506529421004856026300297,
        "PARENT-UUID": 2.436661515947743e+38
    }
]

我希望能够检查具有相同“ URL”的条目,如果发现重复,请将重复的字段“ ACTION”更新为值“ DUPLICATE”,而不是新值。在这种情况下,URL“ https://www.nytimes.com/”会多次出现。 预期的输出将是这样的:

l = [
        {
            "URL": "https://www.nytimes.com/",
            "ACTION": "FOLLOW",
            "RESULTS": "/PAGES/222437976487981212229928695878437391142.png",
            "PAGE-UUID": 2.224379764879812e+38,
            "PARENT-UUID": 2.224379764879812e+38
        },
        {
            "URL": "https://www.nytimes.com/es/",
            "ACTION": "NEW",
            "RESULTS": "/PAGES/138695820074592921946528124297895673746.png",
            "PAGE-UUID": 138695820074592921946528124297895673746,
            "PARENT-UUID": 2.224379764879812e+38
        },
        {
            "URL": "https://www.nytimes.com/",
            "ACTION": "DUPLICATE",
            "RESULTS": "/PAGES/166947399632926520629187111715759306140.png",
            "PAGE-UUID": 166947399632926520629187111715759306140,
            "PARENT-UUID": 2.436661515947743e+38
        },
        {
            "URL": "https://www.nytimes.com/subscriptions/Multiproduct/lp3L3W6.html?campaignId=6W74R",
            "ACTION": "NEW",
            "RESULTS": "/PAGES/299203350572384506529421004856026300297.png",
            "PAGE-UUID": 299203350572384506529421004856026300297,
            "PARENT-UUID": 2.436661515947743e+38
        }
    ]

我尝试使用set,无法完全理解我从这里去哪里

    seen = set()
    new_l = []
    for d in l:
        t = tuple(d.items())
        print "This is t", t
        if t not in seen:
            seen.add(t)
            new_l.append(d)

1 个答案:

答案 0 :(得分:0)

你靠近了!

urls = set()
for d in l:
    if d['URL'] in urls: # A duplicate!
        d['ACTION'] = 'DUPLICATE'
    else: # Not seen before
        ulrs.add(d['URL'])