Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

python - Count letter frequency in word list, excluding duplicates in the same word

I'm trying to find the most frequent letter in a list of words. I'm struggling with the algorithm because I need to count the letter frequency in a word only once skipping duplicates, so I need help finding a way to count the frequency of the letters in the entire list with only one occurrence per word, ignoring the second occurrence.

For example if i have:

words = ["tree", "bone", "indigo", "developer"]

The frequency will be:

letters={a:0, b:1, c:0, d:2, e:3, f:0, g:1, h:0, i:1, j:0, k:0, l:1, m:0, n:2, o:3, p:1, q:0, r:2, s:0, t:1, u:0, v:1, w:0, x:0, y:0, z:0}

As you can see from the letters dictionary: 'e' is 3 and not 5 because if 'e' repeats more than once in the same word it should be ignored.

This is the algorithm that I came up with, it's implemented in Python:

for word in words:
    count=0;

    for letter in word:
        if(letter.isalpha()):
            if((letters[letter.lower()] > 0  && count == 0) ||
               (letters[letter.lower()] == 0 && count == 0)):

                    letters[letter.lower()]+=1
                    count=1

            elif(letters[letter.lower()]==0 && count==1):   
                letters[letter.lower()]+=1

But it still requires work and I can't think about anything else, I'd be glad to anyone who will help me to think about a working solution.

question from:https://stackoverflow.com/questions/54223703/count-letter-frequency-in-word-list-excluding-duplicates-in-the-same-word

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A variation on @Primusa answer without using update:

from collections import Counter

words = ["tree", "bone", "indigo", "developer"]
counts = Counter(c for word in words for c in set(word.lower()) if c.isalpha())

Output

Counter({'e': 3, 'o': 3, 'r': 2, 'd': 2, 'n': 2, 'p': 1, 'i': 1, 'b': 1, 'v': 1, 'g': 1, 'l': 1, 't': 1})

Basically convert each word to a set and then iterate over each set.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...