我正在使用 requests 庫(kù)用 Python 編寫我的機(jī)器人。該機(jī)器人的本質(zhì)是計(jì)算賬戶上所有物品的總金額。一切正常,但金額沒有按照我的需要計(jì)算。API 密鑰作為字典:apikeys = {'account1' : [id1, apikey1], 'account2' : [id2, apikey2], 'account3' : [id3, apikey3]}我的代碼:total = 0for key, value in apikeys.items(): url = request.get('https://market.com/api/items?=key={api}'.format(api=value[1])).json()['items'] for i in url: total += i['price'] JSON 答案:ACCOUNT1 = {'items': [ 'item 1': { 'status': '1', 'price': '10' }, 'item 2': { 'status': '1', 'price': '20' }, 'item 3': { 'status': '1', 'price': '30' }, ACCOUNT2 = {'items': [ 'item 1': { 'status': '1', 'price': '40' }, 'item 2': { 'status': '1', 'price': '50' }, 'item 3': { 'status': '1', 'price': '60' },ACCOUNT3 = {'items': [ 'item 1': { 'status': '1', 'price': '70' }, 'item 2': { 'status': '1', 'price': '80' }, 'item 3': { 'status': '1', 'price': '90' }, 我的機(jī)器人從我獲得的所有帳戶中收集所有物品, 總計(jì) = 450 ( 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 = 450 )total = 450但我需要將每個(gè)帳戶的金額分開:I need:ACCOUNT1 = 10 + 20 + 30 = 60ACCOUNT2 = 40 + 50 + 60 = 150ACCOUNT3 = 70 + 80 + 90 = 40
1 回答

鳳凰求蠱
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
account_total = {}
for key, value in apikeys.items():
url = request.get('https://market.com/api/items?key={api}'.format(api=value[1])).json()['items']
total = 0
for i in url:
total += i['price']
account_total[key] = total
添加回答
舉報(bào)
0/150
提交
取消