Using python, I define the following list of string lists
some_list = [['abc', 'aa', 'xdf'], ['def', 'asd'], ['abc', 'xyz'], ['ghi', 'edd'], ['abc', 'xyz'], ['abc', ]]
What is the best way to select and count the strings that appear in the same sublist than 'abc' ? In this example I am looking for an output (dictionnary or list) such as:
('aa', 1), ('xdf', 1), ('xyz',2), (None, 1)
The (None, 1)
would capture the cases where 'abc'
is the only string of the sublists).
I'd count them all first and then handle the edge case separately:
from collections import Counter
from itertools import chain
c = Counter(chain.from_iterable(x for x in A if 'abc' in x))
c[None] = A.count(['abc'])