简体   繁体   中英

finding the word with most repeated letters from a string containing a sentence in python

I want to find a word with the most repeated letters given an input a sentence. I know how to find the most repeated letters given the sentence but I'm not able how to print the word.

For example:

this is an elementary test example

should print

elementary

def most_repeating_word(strg):
    words =strg.split()
    for words1 in words:
        dict1 = {}
        max_repeat_count = 0
        for letter in words1:
            if letter not in dict1:
                dict1[letter] = 1
            else:
                dict1[letter] += 1
            if dict1[letter]> max_repeat_count:
                max_repeat_count = dict1[letter]
                most_repeated_char = letter
                result=words1
      return result

You are resetting the most_repeat_count variable for each word to 0. You should move that upper in you code, above first for loop, like this:

def most_repeating_word(strg):
    words =strg.split()
    max_repeat_count = 0
    for words1 in words:
        dict1 = {}
        for letter in words1:
            if letter not in dict1:
                dict1[letter] = 1
            else:
                dict1[letter] += 1
            if dict1[letter]> max_repeat_count:
                max_repeat_count = dict1[letter]
                most_repeated_char = letter
                result=words1
    return result

Hope this helps

Interesting exercise! +1 for using Counter() . Here's my suggestion also making use of max() and its key argument, and the * unpacking operator.

For a final solution note that this (and the other proposed solutions to the question) don't currently consider case, other possible characters (digits, symbols etc) or whether more than one word will have the maximum letter count, or if a word will have more than one letter with the maximum letter count.

from collections import Counter

def most_repeating_word(strg):
    # Create list of word tuples: (word, max_letter, max_count)
    counters = [ (word, *max(Counter(word).items(), key=lambda item: item[1]))
                 for word in strg.split() ]
    max_word, max_letter, max_count = max(counters, key=lambda item: item[2])
    return max_word

Use a regex instead. It is simple and easy. Iteration is an expensive operation compared to regular expressions.

Please refer to the solution for your problem in this post: Count repeated letters in a string

word="SBDDUKRWZHUYLRVLIPVVFYFKMSVLVEQTHRUOFHPOALGXCNLXXGUQHQVXMRGVQTBEYVEGMFD"

def most_repeating_word(strg):
    dict={}
    max_repeat_count = 0
    for word in strg:
        if word not in dict:
            dict[word] = 1
        else:
            dict[word] += 1
        if dict[word]> max_repeat_count:
            max_repeat_count = dict[word]
    result={}
    for word, value in dict.items():
        if value==max_repeat_count:
            result[word]=value
    return result

print(most_repeating_word(word))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM