字符串python中一个或多个最常用的字母

时间:2015-07-09 22:31:53

标签: python string

这个问题要求我以字母顺序返回一个小写的字符串,其中最常出现的字母是s。到目前为止,我有:

def mostFrequentLetter(s):
    allchar = ''.join(sorted(s))
    temp = s.replace(" ","")
    max = None
    maxchar = None
    for alph in allchar.lower():
        charcount = temp.count(alph)
        if alph not in string.ascii_letters: continue
        elif charcount > max:
            max = charcount
            max = alph
        elif charcount == max:
            max2 = charcount            
            max2 = alph
            max.append(max2)
    return max

如果我输入'aaaabbbb'它应该给我'ab',但它只给我'a'。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

您可以使用内置collections.Counter

# Change the current path to the last directory used as an argument inside a
# bash command line.
#
# Bash only.
# This function MUST be used at the end of a single command line, like this:
#    $ <any command> ; cdd
# <any command> is any command with any argument(s), including at least one path
# to an existing directory.
# It handles dir names with spaces and quoted dir names
#
# Ex. :
#    /etc $ cp fstab /tmp; cdd            # --> current dir = /tmp
#    /tmp $ ...
#
#    ~ $ mkdir "some proj/src" -p; cdd    # --> current dir = ~/some proj/src
#    ~/some proj/src $ ...
function cdd()
{
    local  last_com  stripped  args  nb_args  arg  last_dir  i

    # Get the current command from the history:
    last_com="$(history|tail -n1|sed -n 's/^\s*[0-9]*\s*//p')"

    # IMPORTANT: if you want to change to name of the function, you have to
    # change it as well in the regex just below:
    # Strip the call of 'cdd' function at the end:
    stripped="$(echo "$last_com"|sed -n 's/;\s*cdd\s*$//p')"

    # Split the command arguments:
    eval "args=( $stripped )"

    nb_args=${#args[@]}
    [[ $nb_args == 0 ]] && return

    # Look for the last directory used as an argument:
    for (( i = nb_args - 1; i != 0; i-- )); do
        arg="${args[$i]}"
        if [[ -d $arg ]]; then
            last_dir="$arg"
            break
        fi
    done

    # If found, change current directory:
    [[ -n $last_dir ]] && cd "$last_dir"
}

如果由于某种原因您无法使用from collections import Counter def most_frequent_letter(s): counter = Counter(s) letter, max_count = next(counter.most_common()) letters = sorted(letter for letter, count in counter.most_common() if count == max_count) return ''.join(letters) ,则可以使用default dictionary

Counter

答案 1 :(得分:0)

我建议您使用set()功能,这样您就不会多次检查重复的字符。

def mostFrequentLetter(s):
    return ''.join(sorted(sorted((c for c in set(s) if c in string.ascii_letters), key=s.count, reverse=True)[:2]))

首先将字符串转换为set,从而消除重复。然后,它会根据每个元素在原始字符串中出现的频率(反向(降序))对set进行排序。最后,它将已排序的元素连接成一个字符串并返回它。

>>> s = 'abbbbbbbbccddddddddddddddddd'
>>> mostFrequentLetter(s)
'bd'

答案 2 :(得分:0)

其他答案更诡异。对于您的代码,虽然您犯了一些错误。请参阅以下代码的注释

def mostFrequentLetter(s):
    allchar = ''.join(sorted(s))
    temp = s.replace(" ","")
    max = None
    maxchar = ""
    for alph in set(allchar.lower()):
        charcount = temp.count(alph)
        if alph not in string.ascii_letters: continue
        elif charcount > max:
            max = charcount
            maxchar+=alph #max = alph sets max as alph. You need to
                          #append the maximum occuring character, not set it as max
        elif charcount == max:
            maxchar+=alph  # max2 is not really needed. Its not being used anywhere else.
                           # In fact this whole clause can be refactored by setting charcount >= max above.
                           # I am still leaving it to be in line with what you wrote
    return maxchar

print mostFrequentLetter("aaaabbbb")

答案 3 :(得分:0)

这里,解决方案适用于您提供的代码而不使用Set:)

def mostFrequentLetter(s):
    allchar = ''.join(sorted(s))
    temp = s.replace(" ","")
    max = None
    maxchar = None
    for alph in allchar.lower():
        charcount = temp.count(alph)
        if alph not in string.ascii_letters: continue
        elif charcount > maxchar:
            maxchar= charcount
            max= alph
        elif charcount == maxchar and alph not in max:          
            max+=alph
    return max