Home > Enterprise >  [Replace duplicate value - Python list]
[Replace duplicate value - Python list]

Time:08-15

Input:

data = [
    {'name': 'A', 'value': 19, 'no': 1}, 
    {'name': 'B', 'value': 5, 'no': 2},
    {'name': 'A', 'value': 19, 'no': 3}
] 
request_change_data = [
    {'name': 'A', 'value': 35, 'no': 1},
    {'name': 'B', 'value': 10, 'no': 2},
    {'name': 'A', 'value': 40, 'no': 3}
]

expected_result:

data = [
    {'name': 'A', 'value': 35, 'no': 1},
    {'name': 'B', 'value': 10, 'no': 2},
    {'name': 'A', 'value': 40, 'no': 3}
]

But actual:

[
    {'name': 'A', 'value': 40, 'no': 1},
    {'name': 'B', 'value': 10, 'no': 2},
    {'name': 'A', 'value': 40, 'no': 3}
]

My code is:

data = [{'name': 'A', 'value': 19, 'no': 1}, {'name': 'B', 'value': 5, 'no': 2}, {'name': 'A', 'value': 19, 'no': 3}] 

requests = [{'name': 'A', 'value': 35, 'no': 1}, {'name': 'B', 'value': 10, 'no': 2}, {'name': 'A', 'value': 40, 'no': 3}]

def test(data, requests):
    for k, v in enumerate(data):
        for request in requests:
            if v['name'] == request['name']:
                v['value'] =request['value']
    return data

print(test(data, requests))

How could I change the duplicate stt1 và stt3. I used for to update the value of the key, it always updates only stt3 value is 40. Please help. Thanks in advance

CodePudding user response:

Each time you iterate through data, you then iterate over all of the request dictionaries, and your code only checks the name fields for each dictionary and then updates the value field in the dict from data if they match.

However, you have multiple dictionaries in requests with the same name, so if you were working the first data dict:

{'name': 'A', 'value': 19, 'no': 1}

You'd get this in for request in requests:

Iteration 1: request = {'name': 'A', 'value': 35, 'no': 1},
Iteration 2: request = {'name': 'B', 'value': 10, 'no': 2},
Iteration 3: request = {'name': 'A', 'value': 40, 'no': 3}

So you'd end up updating the data dict twice, first with v['value'] = 35 and then with v['value'] = 40.

So for your data, you want to check both name and no in the dicts and if they both match, then update the fields. Here's a fixed version of your code that does that:

data = [{'name': 'A', 'value': 19, 'no': 1}, {'name': 'B', 'value': 5, 'no': 2}, {'name': 'A', 'value': 19, 'no': 3}]

requests = [{'name': 'A', 'value': 35, 'no': 1}, {'name': 'B', 'value': 10, 'no': 2}, {'name': 'A', 'value': 40, 'no': 3}]

# You didn't seem to need the idx from enumerating so I removed it
# You also don't need to return data because lists/dicts are mutable
# types so you're modifying the actual dicts you pass in
def test(data, requests):
    for d in data:
        for request in requests:
            if d['name'] == request['name'] and d['no'] == request['no']:
                d['value'] = request['value']

test(data, requests)
print(data)

And I get this output, which is your expected:

[
    {'name': 'A', 'value': 35, 'no': 1},
    {'name': 'B', 'value': 10, 'no': 2},
    {'name': 'A', 'value': 40, 'no': 3}
]
  • Related