第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從包含python句子的字符串中查找重復(fù)字母最多的單詞

從包含python句子的字符串中查找重復(fù)字母最多的單詞

慕桂英4014372 2023-08-22 10:15:58
我想在輸入的句子中找到重復(fù)字母最多的單詞。我知道如何找到句子中重復(fù)次數(shù)最多的字母,但我無法打印該單詞。例如:這是一個(gè)基本的測試示例應(yīng)該打印初級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
查看完整描述

4 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

您正在將每個(gè)單詞的most_repeat_count變量重置為0。您應(yīng)該將代碼中的上部移動(dòng)到第一個(gè)for循環(huán)的上方,如下所示:


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


查看完整回答
反對 回復(fù) 2023-08-22
?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊

請改用正則表達(dá)式。它簡單又容易。與正則表達(dá)式相比,迭代是一項(xiàng)昂貴的操作。

查看完整回答
反對 回復(fù) 2023-08-22
?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

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))


查看完整回答
反對 回復(fù) 2023-08-22
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

有趣的練習(xí)!使用+1 Counter()。這是我的建議,還利用了max()它的key參數(shù)以及*解包運(yùn)算符。


對于最終解決方案,請注意,此解決方案(以及該問題的其他建議解決方案)當(dāng)前不考慮大小寫、其他可能的字符(數(shù)字、符號(hào)等)或是否多個(gè)單詞具有最大字母數(shù),或者如果一個(gè)單詞將有多個(gè)字母且具有最大字母數(shù)。


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


查看完整回答
反對 回復(fù) 2023-08-22
  • 4 回答
  • 0 關(guān)注
  • 280 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)