Python- Count each letter in a list of words

后端 未结 7 1514
心在旅途
心在旅途 2021-01-16 05:46

So I have a list of words `wordList = list().\' Right now, I am counting each letter in each of the words throughout the whole list using this code

cnt = C         


        
相关标签:
7条回答
  • 2021-01-16 06:30

    This creates a set from each word and passes them to the constructor of Counter.

    >>> from itertools import chain, imap
    >>> from operator import itemgetter
    >>> from collections import Counter
    >>> words = 'happy', 'harpy', 'hasty'
    >>> counter = Counter(chain.from_iterable(imap(set, words)))
    >>> map(itemgetter(0), counter.most_common())
    ['a', 'h', 'y', 'p', 's', 'r', 't']
    
    0 讨论(0)
提交回复
热议问题