Search code examples
pythonarraylistlist-comprehensionnested-lists

Compare Nested List With Different Index Amount and Same Condition in Particular List


i have two nested list and the result

data1 = [
    ["tr1", 1002, 6798381], # return false
    ["tr1", 1001, 6798381], # return false
    ["tr1", 1001, 6798381], # return false
    ["tr1", 1000, 6798381], # return vid1
    ["tr1", 1000, 6798381], # return vid2 (can be vid1, but vid1 already used) | But I got False
    ["tr2", 1200, 6798382], # vid3
    ["tr4", 1200, 6798384], # vid5
    ["tr5", 1200, 6798385], # false
    ["tr3", 1200, 6798383], # vid4
]

data2 = [
    ["tr1", 1000, 6798381, "vid1"],
    ["tr1", 1000, 6798381, "vid2"],
    ["tr2", 1200, 6798382, "vid3"],
    ["tr3", 1200, 6798383, "vid4"],
    ["tr4", 1200, 6798384, "vid5"]
] 

i need to move data2 last index(data2[-1] which is vid1 or vid2 etc.) into data1 if value is same, but if data2 vid already taken, i should delete so i dont get multiple data from data2

Code I use

for x in data1:
    key = (x[0], x[1], x[2])
    if key in data2_dict:
        print(data2_dict[key])
        # Remove the key from data2_dict to prevent reuse
        del data2_dict[key]
    else:
        print("false")

output i get from my code

    false
    false
    false
    vid1
    False (should be vid1 or vid2, but vid1 already used so should be vid2)
    vid3
    vid5
    false
    vid4

output i expect

    false
    false
    false
    vid1
    vid2
    vid3
    vid5
    false
    vid4

or

false
    false
    false
    vid2 --
    vid1 --
    vid3
    vid5
    false
    vid4

Edit Expect result : instead of print vid/false can you move into data1 ?

["tr1", 1002, 6798381], # return false
    ["tr1", 1001, 6798381, 'false'], # return false
    ["tr1", 1001, 6798381, 'false'], # return false
    ["tr1", 1000, 6798381, 'vid1'], # return vid1
    ["tr1", 1000, 6798381, 'vid2'], # return vid2 (can be vid1, but vid1 already used) | But I got False
    ["tr2", 1200, 6798382, 'vid3'], # vid3
    ["tr4", 1200, 6798384, 'vid5'], # vid5
    ["tr5", 1200, 6798385, 'false'], # false
    ["tr3", 1200, 6798383, 'vid4'], # vid4

can you guys help me? thanks


Solution

  • Since vid1 and vid2 share the same key, it's best to build a dict of lists to keep track of vids of the same keys. When iterating keys from data1, pop the vids from the sub-lists of the current keys to prevent reuse. Output 'false' if KeyError is raised from a non-existent key or if IndexError is raised from an exhausted sub-list:

    data = {}
    for *key, vid in data2:
        data.setdefault(tuple(key), []).append(vid)
    output = []
    for key in map(tuple, data1):
        try:
            vid = data[key].pop()
        except (KeyError, IndexError):
            vid = 'false'
        output.append([*key, vid])
    

    output becomes:

    [['tr1', 1002, 6798381, 'false'],
     ['tr1', 1001, 6798381, 'false'],
     ['tr1', 1001, 6798381, 'false'],
     ['tr1', 1000, 6798381, 'vid2'],
     ['tr1', 1000, 6798381, 'vid1'],
     ['tr2', 1200, 6798382, 'vid3'],
     ['tr4', 1200, 6798384, 'vid5'],
     ['tr5', 1200, 6798385, 'false'],
     ['tr3', 1200, 6798383, 'vid4']]
    

    Demo: Try it online!