[英]Python 3.6 iterating through list of dictionaries using filter() with lambda doesn't return dictionary's value
[英]Python list of dictionaries: How to tell if value doesn't exist
我有一個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}]
之后,我從db中提取要修改的數據,並將其與從JSON響應中提取的數據進行匹配。
如果db中存在“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”附加一個新的列表條目,但是輸出是print的無限循環(“不存在”)。
我最接近解決方案的是:
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
}]
db中的數據
活動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
}]
重新分組您的數據,以便您擁有由目標鍵入的字典。
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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.