Home > Blockchain >  getting unique values and their following pairs from list of tuples
getting unique values and their following pairs from list of tuples

Time:01-27

I have a list of tuples like this:

[
    ('a', 'AA'), # pair 1

    ('d', 'AA'), # pair 2
    ('d', 'a'),  # pair 3
    ('d', 'EE'), # pair 4

    ('b', 'BB'), # pair 5
    ('b', 'CC'), # pair 6
    ('b', 'DD'), # pair 7

    ('c', 'BB'), # pair 8
    ('c', 'CC'), # pair 9
    ('c', 'DD'), # pair 10

    ('c', 'b'),  # pair 11

    ('d', 'FF'), # pair 12

]

each of the tuple in the list above shows a similar pair of items(or duplicate items). I need to create a dictionary in which, keys will be one of the unique item from the tuples and values will be lists filled with all the other items that the key occurred in conjunction with. for example, 'a' is similar to 'AA'(pair 1), which in turn is similar to 'd'(pair 2) and 'd' is similar to 'EE' and 'FF' (pairs 4 and 12). same is the case with other items.

My expected output is:

{'a':['AA', 'd', 'EE', 'FF'], 'b':['BB', 'CC', 'DD', 'c']}

according to the tuples, ['a', 'AA', 'd', 'EE', 'FF'] are similar. so, any one of them can be the key, while the remaining items will be it's values. so, output can also be: {'AA':['a', 'd', 'EE', 'FF'], 'c':['BB', 'CC', 'DD', 'b']}. so, key of the output dict can be anything from the duplicate pairs.

How do I do this for a list with thousands of such tuples in a list?

CodePudding user response:

You can use 3 dictionaries, one for the output (out), one to track the seen values (seen), and one to map the equal keys (mapper):

out = {}
seen = {}
mapper = {}

for a, b in l:
    if b in seen:
        out.setdefault(seen[b], []).append(a)
        mapper[a] = seen[b]
    else:
        out.setdefault(mapper.setdefault(a, a), []).append(b)
        seen[b] = a

# remove duplicates
out = {k: list(dict.fromkeys(v)) for k, v in out.items()}

Output:

{'a': ['AA', 'd', 'a', 'EE', 'FF'],
 'b': ['BB', 'CC', 'DD', 'c', 'b']}
  • Related