Home > database >  How to count frequency of first letters of strings?
How to count frequency of first letters of strings?

Time:11-12

I have a list of strings in my program, for example:

[ 'home', 'dog', 'park', 'house', 'hotel', 'fire' ]

and I want to know which is the most first frequent letter .

for example in that string is the letter H because it is in hotel, house and home.

I tried just 2 for cycles but I didn't get the result

CodePudding user response:

You could create a list of the first letters and then use the max() function to get the highest number of occurrences in it.

lst = ['home', 'dog', 'park', 'house', 'hotel', 'fire']
max([item[0] for item in lst], key=lst.count)

output

'h'

CodePudding user response:

Extracting first elemnt from list item & Using collections

from collections import Counter
ls= [ 'home', 'dog', 'park', 'house', 'hotel', 'fire' ]
ls = [w[0] for w in ls]

s=(Counter(ls))

print(max(s, key=s.get))

output #

h

CodePudding user response:

You can get the first letter of each work with a list comprehension.

Then you can use collections.Counter to find the frequency of each letter and find the maximum of the returned dictionary:

import collections

words = [ 'home', 'dog', 'park', 'house', 'hotel', 'fire' ]

first_letters = [w[0] for w in words]

f = collections.Counter(first_letters)

print(max(f, key=f.get))

Output: h

CodePudding user response:

You could create a list of the first character of each string and then use python's statistics module like so:

from statistics import mode
def most_common_first_letter(words):
    first_letters = []
    for word in words:
        first_letters.append(word[0])
    return mode(first_letters)
    

This uses the mode function to find the most common letter in the list of first letters.

CodePudding user response:

You can use Counter from collections module, for example:

from collections import Counter


list_ = ['home', 'dog', 'park', 'house', 'hotel', 'fire']


if __name__ == '__main__':
    counter = Counter(word[0] for word in list_)
    print(counter.most_common(1)[0][0])

CodePudding user response:

i'm at begining so my code don't use other library function.
it may not be shorter than other, but if you solving an exercise you may found it usefull

if __name__ == '__main__':
imput_list = [ 'home', 'dog', 'park', 'house', 'hotel', 'fire' ]

# create a list of alphabet letter
letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

#sort the input to make the problem easier
imput_list.sort()

#elaborate the data
count = []
for i in letter:
    counter = 0
    while imput_list and imput_list[0][0].lower() == i:
        imput_list.pop(0)
        counter  = 1
    count.append([i, counter])
print(count)
print('')

#ordering the list
count.sort(key= lambda x: x[1], reverse= True)
print(count)
print('')

maximun_letter = count[0][0]
print(maximun_letter)

output:

[['a', 0], ['b', 0], ['c', 0], ['d', 1], ['e', 0], ['f', 1], ['g', 0], ['h', 3], ['i', 0], ['j', 0], ['k', 0], ['l', 0], ['m', 0], ['n', 0], ['o', 0], ['p', 1], ['q', 0], ['r', 0], ['s', 0], ['t', 0], ['u', 0], ['v', 0], ['w', 0], ['x', 0], ['y', 0], ['z', 0]]

[['h', 3], ['d', 1], ['f', 1], ['p', 1], ['a', 0], ['b', 0], ['c', 0], ['e', 0], ['g', 0], ['i', 0], ['j', 0], ['k', 0], ['l', 0], ['m', 0], ['n', 0], ['o', 0], ['q', 0], ['r', 0], ['s', 0], ['t', 0], ['u', 0], ['v', 0], ['w', 0], ['x', 0], ['y', 0], ['z', 0]]

h

  • Related