-1

I need to count how many excact letter in a word. E.g i need to count the letter "i" in these word. And i need to find which word has the most "i" letter in it.


list = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
def fgv(list):
 counter= 0
 maximal = 0
 word= "x"
 for e in list:
 counter = 0
 if e.__contains__("i"):
 for i in range (len(e)):
 if e[i] == "i":
 couner += 1
 if counter < maximal:
 maximal = counter
 word = e
 return(word) 
print(f"{fgv(list)}")
asked Jul 2, 2023 at 14:26
2
  • [w.count('i') for w in list][0, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 0] Commented Jul 2, 2023 at 14:29
  • In python, strings have a count() method which seems to be what you are looking for, Commented Jul 2, 2023 at 14:29

4 Answers 4

0

One more approach which is doing too much work for your problem, but shows off the stdlib's collections.Counter object which is very useful for a bunch of other applications!

from collections import Counter
words = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
word_counts = [Counter(word) for word in words]
# word_counts is now a list of Counter objects, which each look a little bit
# like {"b": 1, "o": 1, "x": 1} for box
# or {"i": 1, "l": 3, "e": 1, "g": 1, "a": 1} for illegal
# now we'll get the index for the word with the most "i's"
idx_max_by_i = max(enumerate(word_counts), key=lambda pair: pair[1]['i'])[0]
# and convert that back to the word itself by indexing into the words list
result = words[idx_max_by_i]
answered Jul 2, 2023 at 16:14
Sign up to request clarification or add additional context in comments.

1 Comment

There are definitely better ways to accomplish this, but I thought it was useful to show off what Counter can do. Note its more powerful methods like most_common(n) which show the most common N number of elements, or elements which give back the "exploded" view of the elements in the counter (e.g. Counter('sheep').elements() produces something like "eehps")
0

If you are only interested in one (the first) word that has a most amount of i's in case of a tie, then this functional one-liner may be desirable:

lst = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
print(max(lst, key=lambda x: x.count("i")))
answered Jul 2, 2023 at 14:30

1 Comment

@juanpethes consider storing the result of word.count('i') for each word and iterating over the zip of lst and word_counts in your final list comp
-1
def count_letter(list):
 counter = 0
 maximal = 0
 word = ""
 for e in list:
 counter = e.count("i")
 if counter > maximal:
 maximal = counter
 word = e
 return word
word_list = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
print(count_letter(word_list))

try this code

answered Jul 2, 2023 at 14:28

4 Comments

This only prints the first occurrence of a one with max amount of i's, if there is a tie. Not necessarily wrong, I just point that out.
Welcome to Stack Overflow! You've posted 9 answers so far over the last 24 hours, and most of them are either mostly code with no explanation, appear likely to have been written (entirely or partially) by AI (e.g., ChatGPT), or both. A heads-up that posting AI-generated content is not allowed here. If you used an AI tool to assist with any answer, I would encourage you to delete it. For answers where you have not used AI, keep in mind that providing additional context regarding why and/or how your code answers the question improves its long-term value.
Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation. If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly.
This is poor advice. 1) variable named list is a no-no. 2) If the input list is empty the return value will be an empty string. But there was no empty string in the list! 3) It will only ever return the first observed word matching the condition - there may be multiples
-1

If you consider that dictionaries can be keyed on integers then how about:

_list = ['box', 'computer', 'sheep', 'family', 'illegal', 'bubble', 'lamp', 'circuit', 'building', 'plant', 'these', 'cup']
def get_count(list_of_words: list[str], letter: str) -> list[str]|None:
 if list_of_words and len(letter) == 1:
 d = {}
 for word in list_of_words:
 d.setdefault(word.count(letter), []).append(word)
 return d[max(d)]
 
print(get_count(_list, 'i'))

Output:

['circuit', 'building']
answered Jul 2, 2023 at 15:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.