Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

Add dicts with identical keys to list without overwriting values in Python

I have the following problem: In a loop over a input parameter list I make an API call for each list entry. I want to then write the results into a dict and append that dict to an output list. At the end of the loop I want to print that output list. What happens is that the last API call / the last element in the input parameter list is overwritten every time I append to the output list.

import requests

input_parameters = ['A', 'B', 'C', 'D']

output = []
record = {}

for input in input_parameters:
    r=requests.get("https://api.domain.com/v2/param=" + str(input))

    record['id'] = str(r.json()['data'][0]['id'])
    record['descr'] = str(r.json()['data'][0]['descr'])
    record['qty'] = str(r.json()['data'][0]['qty'])

    output.append(record)

print(output)

The output I get:

[{'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}]

The output I would like:

[{'id': 'A', 'descr': 'barfoo', 'qty': '50'}, {'id': 'B', 'descr': 'bar or foo', 'qty': '80'}, {'id': 'C', 'descr': 'foo foo bar', 'qty': '25'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}]

Your help is much appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

output.append(record) does not copy record. So, when you then do, say, record['id'] = str(r.json()['data'][0]['id']), the original, one and only record will be modified.

Since there's only one record object, successive calls output.append(record) will populate output with references to the same dictionary object.

You can instead create a new dictionary each time:

record = {
    'id': str(r.json()['data'][0]['id']),
    'descr': str(r.json()['data'][0]['descr'])
    'qty': str(r.json()['data'][0]['qty'])
}

Or copy the dictionary with output.append(record.copy()). In any case, you'll need to have a new dictionary object for each element of input_parameters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...