简体   繁体   中英

count all the tuple elements in a nested list

I want to get the count of all the elements in a nested list. nested_lst = [[('NOUN', 'shoe'), ('NOUN', 'shirt'), ('NOUN', 'jacket')], [('VERB', 'jump')], [('NOUN', 'shoe'), ('NOUN', 'shirt'), ('ADJ', 'fancy')]]

My expected outcome is:

{'NOUN': 5, 'shoe': 2, 'shirt':2, 'VERB': 1, 'jump': 1, 'jacket': 1, 'ADJ': 1, 'fancy':1}

My current code: Counter(itertools.chain(*nested_lst)) which produced the count of tuples instead of each element.

Apply chain two times to get rid of inner tuples

from itertools import chain
from collections import Counter
Counter(chain(*chain(*nested_lst)))

Or

Counter(chain.from_iterable(chain.from_iterable(nested_lst)))

Output:

Counter({'ADJ': 1,
         'NOUN': 5,
         'VERB': 1,
         'fancy': 1,
         'jacket': 1,
         'jump': 1,
         'shirt': 2,
         'shoe': 2})

You are very close. You just need another iteration through values returned by chain to get desired output:

from collections import Counter
from itertools import chain

nested_lst = [[('NOUN', 'shoe'), ('NOUN', 'shirt'), ('NOUN', 'jacket')], [('VERB', 'jump')], [('NOUN', 'shoe'), ('NOUN', 'shirt'), ('ADJ', 'fancy')]]

print(Counter(y for x in chain.from_iterable(nested_lst) for y in x))
# Counter({'NOUN': 5, 'shoe': 2, 'shirt': 2, 'jacket': 1, 'VERB': 1, 'jump': 1, 'ADJ': 1, 'fancy': 1})
In [2]: for i in nested_lst:
   ...:     for j in i:
   ...:         for k in j:
   ...:             d[k] = d.setdefault(k, 0) + 1
   ...:

In [3]: d
Out[3]:
{'ADJ': 1,
 'NOUN': 5,
 'VERB': 1,
 'fancy': 1,
 'jacket': 1,
 'jump': 1,
 'shirt': 2,
 'shoe': 2}

I your list is only nested once, you can get a flat list with sum() . After this you can make an empty dictionary, and increase the value in each iteration, where the word appears. If there is a new word, default the value to zero, and one will be added anyways. Most likely not the most efficient solution though.

nested_lst = [[('NOUN', 'shoe'), ('NOUN', 'shirt'), ('NOUN', 'jacket')], [('VERB', 'jump')], [('NOUN', 'shoe'), ('NOUN', 'shirt'), ('ADJ', 'fancy')]]
flat_lst = sum(nested_lst, [])
dct = {}
for typ, name in flat_lst:
    dct[typ] = 1 + dct.setdefault(typ, 0)
    dct[name] = 1 + dct.setdefault(name, 0)

# {'NOUN': 5, 'shoe': 2, 'shirt':2, 'VERB': 1, 'jump': 1, 'jacket': 1, 'ADJ': 1, 'fancy':1}
print(dct)

You could always step through each list of tuples and add the counts with Counter.update() :

from collections import Counter
from itertools import chain

nested_lst = [[('NOUN', 'shoe'), ('NOUN', 'shirt'), ('NOUN', 'jacket')], [('VERB', 'jump')], [('NOUN', 'shoe'), ('NOUN', 'shirt'), ('ADJ', 'fancy')]]

counts = Counter()
for lst in nested_lst:
    counts.update(Counter(chain.from_iterable(lst)))

print(counts)
# Counter({'NOUN': 5, 'shoe': 2, 'shirt': 2, 'jacket': 1, 'VERB': 1, 'jump': 1, 'ADJ': 1, 'fancy': 1})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM