[Python] Python- Count each letter in a list of words

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    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 = Counter()
    for words in wordList:
    for letters in words:
    cnt[letters]+=1


    However, I want it to count differently. I want the function to find the most common letter out of all the words in the list, but only by counting each letter per word once (ignoring the fact that some words can have multiple copies of the same letter).

    For example, if the list contained 'happy, harpy and hasty', the two p's in happy should only be counted once. So the function should return a list of the highest frequency letters (in order) without double counting. In the above case it would be 'h, a, p, y, r, s"

    Continue reading...

Compartilhe esta Página