Suppose I have two lists:
l1 = [['b', (1, 1)], ['b', (1, 2)], ['b', (1, 3)], ['a', (1, 5)],
['b', (2, 1)], ['b',(3, 1)]]
l2 = ['A','B','C']
How can I create a dictionary into this format?
dct = {'A': len(sublist1), 'B': len(sublist2), 'C' : len(sublist3)}
where
sublist1 = [['b', (1, 1)], ['b', (1, 2)], ['b', (1, 3)], ['a', (1, 5)]]
sublist2 = [['b', (2, 1)]]
sublist3 = [['b',(3, 1)]]
what will happen if my l1 is as given below:
ls1 = [[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2)]]
then my output should be:
dct = {'A': len(sublist1), 'B': len(sublist2)}
where
sublist1 = [[(1, 1),(1, 2),(1, 3),(1, 4)]]
sublist2 = [[(2, 1),(2, 2),(2, 3)]]
Can the overall problem can be solved in generic way?
This seems to work:
from itertools import groupby
key = lambda x: x[1][0]
lens = [len(list(g)) for k, g in groupby(sorted(l1, key=key), key=key)]
dct = dict(zip(l2, lens))
I hope I've inferred correctly when I've assumed A match with 1, B with 2, and so on.
Re: OP edit
I don't know where the (2, 3)
item in your sublist2
comes from, I assume it's a mistake. Also, I assume the single element list ls1
was in fact meant to be the direct container of the tuples, since using nested lists here serves no purpose. If so, here's my proposal for a generic solution:
from itertools import groupby
from string import ascii_uppercase
key = lambda x: x[0]
lens = [len(list(g)) for k, g in groupby(sorted(l1, key=key), key=key)]
dct = dict(zip(ascii_uppercase, lens))
So, no big changes. The zip
function will, when given arguments of unequal length, return a list with the same length as the shortest argument, and this behaviour suits us fine in this situation.
Keep in mind that if there are more than 26 different values of the first tuple element, then this solution will break, and simply disregard any values larger than the 26th.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With