首页 > 解决方案 > Python 字典列表:如何判断值是否不存在

问题描述

我有一个 JSON 查询,它返回我要修改的字典列表。这是我在回复中感兴趣的部分:

{
    #...
    "publisher_bid_modifier": {
        "values": [{
                "target": "msn-can",
                "cpc_modification": 1.5
            },
            {
                "target": "msn-can-home",
                "cpc_modification": 1.5
            }
        ]
    }
}

下面代码中的 print(temp) 将返回:

[{"target": "msn-can-home","cpc_modification": 0.5}, {"target": "msn-can","cpc_modification": 0.5}]

之后,我从数据库中提取要修改的数据,并将其与从 JSON 响应中提取的数据进行匹配。

如果数据库中存在“target”的值,我可以轻松修改“cpc_modification”。我的问题是当响应中不存在“目标”值时能够做其他事情。

这是我到目前为止所做的:

print(temp)

for dt in temp:
    theCursor.execute('SELECT ADJUST, SITE FROM SandboxPubTB WHERE CAMPAIGNNAME =?', (campaignName,) ) 
    campaignRows = theCursor.fetchall()
    for t in campaignRows:

        if t[1] == dt['target'] :
            dt['cpc_modification'] = "{:.2f}".format((int(t[0]) / 100) + 1)
            print("exists")
        #if dt['target'] not in t[1] :
        else:
            temp.append({'target': '"' + t[1] + "'", 'cpc_modification': "'" + str(t[0]) + "'"})
            print("Doesn't exists") 

print(temp) 

在 else 中,我试图用新的“target”和“cpc_modification”附加一个新的列表条目,但输出是打印的无限循环(“不存在”)。

我最接近解决方案的是:

elif dt['target'] not in temp:

但这将迭代与临时列表中的条目数一样多的时间。

举一个输入和输出的例子:

输入:

[{
    'target': 'msn-can',
    'cpc_modification': 1.5
}, {
    'target': 'msn-can-home',
    'cpc_modification': 1.5
}, {
    'target': 'foxnews-foxnews',
    'cpc_modification': 1.5
}, {
    'target': 'beachraider',
    'cpc_modification': 0.69
}, {
    'target': 'crowdyfan',
    'cpc_modification': 0.7
}, {
    'target': 'novelodge',
    'cpc_modification': 0.75
}, {
    'target': 'foxnews-androidapp',
    'cpc_modification': 0.5
}, {
    'target': 'foxnews-foxbusiness',
    'cpc_modification': 1.12
}, {
    'target': 'foxnews-iosapp',
    'cpc_modification': 0.86
}, {
    'target': 'thehindu-hindunews',
    'cpc_modification': 0.7
}, {
    'target': 'vitaminnews',
    'cpc_modification': 1.46
}]

数据库:

在此处输入图像描述

输出:

[{
    'target': 'msn-can',
    'cpc_modification': 0.5 <----
}, {
    'target': 'msn-can-home',
    'cpc_modification': 0.5 <----
}, {
    'target': 'foxnews-foxnews',
    'cpc_modification': 1.5
}, {
    'target': 'beachraider',
    'cpc_modification': 0.69
}, {
    'target': 'crowdyfan',
    'cpc_modification': 0.7
}, {
    'target': 'novelodge',
    'cpc_modification': 0.75
}, {
    'target': 'foxnews-androidapp',
    'cpc_modification': 0.5
}, {
    'target': 'foxnews-foxbusiness',
    'cpc_modification': 1.12
}, {
    'target': 'foxnews-iosapp',
    'cpc_modification': 0.86
}, {
    'target': 'thehindu-hindunews',
    'cpc_modification': 0.7
}, {
    'target': 'vitaminnews',
    'cpc_modification': 1.46
}], {
    'target': 'msn-outlookcom-canada', <----
    'cpc_modification': 0.5 <----
}]

您的帮助将不胜感激。谢谢!

编辑

上面的代码只处理了一个活动 *campaignName 变量”,但整个代码应该能够处理多个活动。所以这里是另一个例子:

输入

活动 1:

[{
    'target': 'msn-can',
    'cpc_modification': 0.5
}, {
    'target': 'msn-can-home',
    'cpc_modification': 0.5
}]

活动 2:

[{
    'target': 'fox-news',
    'cpc_modification': 0.9
}, {
    'target': 'fox-news-home',
    'cpc_modification': 0.6
}]

数据库中的数据

活动 1:

target: msn-can, cpc_modification: 7
target: msn-can-home, cpc_modification: 10

活动 2:

target: fox-news, cpc_modification: 20
target: fox-news-home, cpc_modification: 30
target: msn-us, cpc_modification: 10

输出

活动 1:

[{
    'target': 'msn-can',
    'cpc_modification': 1.07
}, {
    'target': 'msn-can-home',
    'cpc_modification': 1.1
}]

活动 2:

[{
    'target': 'fox-news',
    'cpc_modification': 1.2
}, {
    'target': 'fox-news-home',
    'cpc_modification': 1.3
}], {
    'target': 'msn-us',
    'cpc_modification': 1.1
}]

标签: pythonlist

解决方案


重新分组您的数据,以便您拥有由目标键入的字典。

rekey = {t['target']: t for t in temp}

然后你可以自然地寻找你的数据

theCursor.execute('SELECT ADJUST, SITE FROM SandboxPubTB WHERE CAMPAIGNNAME =?', (campaignName,) ) 
campaignRows = theCursor.fetchall()
for t in campaignRows:
    fmt_value = "{:.2f}".format((int(t[0]) / 100) + 1)
    try:
        print(f"updating {t[1]}")
        rekey[t[1]]['cpc_modification'] = fmt_value
    except KeyError:
        print(f"Adding new key {t[1]}")
        rekey[t[1]] = {'target': t[1], 'cpc_modification': fmt_value}

from pprint import pprint
pprint(rekey.values())

推荐阅读