fgypas
fgypas

Reputation: 326

Python mapping between lists for duplicates

I have two parallel lists like the following:

List1 - List2
1 -------- A
1 -------- B
1 -------- C
2 -------- D
2 -------- D
2 -------- D
2 -------- E
2 -------- A
3 -------- A
3 -------- L
3 -------- M

I am looking for a fast way in python to count how many attributes of list 2 map to different attributes in list 1. In this example I would like to get an ouput like:

A maps to 1, 2, 3

Upvotes: 0

Views: 62

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121614

Use zip() to pair up the values from both lists and collections.defaultdict() to produce a mapping:

from collections import defaultdict

mapping = defaultdict(set)
for v1, v2 in zip(List1, List2):
    mapping[v2].add(v1)

Now you have a dictionary mapping values from list 2 to sets containing unique values from list 1; you can print these to match your sample output with:

for v2 in sorted(mapping):
    print '{} maps to {}'.format(v2, ', '.join(map(str, sorted(mapping[v2]))))

For your sample input, this produces:

>>> mapping
defaultdict(<type 'set'>, {'A': set([1, 2, 3]), 'C': set([1]), 'B': set([1]), 'E': set([2]), 'D': set([2]), 'M': set([3]), 'L': set([3])})
>>> for v2 in sorted(mapping):
...     print '{} maps to {}'.format(v2, ', '.join(map(str, sorted(mapping[v2]))))
... 
A maps to 1, 2, 3
B maps to 1
C maps to 1
D maps to 2
E maps to 2
L maps to 3
M maps to 3

Upvotes: 2

ndpu
ndpu

Reputation: 22561

Use defaultdict with zip and set:

>>> l1 = [1,1,1,2,2,2,2,2,3,3,3]
>>> l2 = ['a','b','c','d','d','d','e','a','a','l','m']
>>> result = defaultdict(set)
>>> for v,k in zip(l1,l2):
...     result[k].add(v)
... 
>>> result
defaultdict(<type 'set'>, {'a': set([1, 2, 3]), 'c': set([1]), 'b': set([1]),
                  'e': set([2]), 'd': set([2]), 'm': set([3]), 'l': set([3])})
>>> list(result['a'])
[1, 2, 3]

Upvotes: 1

Related Questions