Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找给定字符串中最小字符数的Python代码_Python - Fatal编程技术网

查找给定字符串中最小字符数的Python代码

查找给定字符串中最小字符数的Python代码,python,Python,我需要一段代码来查找给定字符串中字母的最小出现次数 我不能使用min函数。我已经有了一个函数来查找字母的最大出现次数。我需要对代码进行哪些修改 def getMaxOccuringChar(str): ASCII_SIZE = 256 count = [0] * ASCII_SIZE max = -1 c = '' for i in str: count[ord(i)] += 1; for i in str: i

我需要一段代码来查找给定字符串中字母的最小出现次数

我不能使用min函数。我已经有了一个函数来查找字母的最大出现次数。我需要对代码进行哪些修改

def getMaxOccuringChar(str):

    ASCII_SIZE = 256
    count = [0] * ASCII_SIZE
    max = -1
    c = ''
    for i in str:
        count[ord(i)] += 1;

    for i in str:
        if max < count[ord(i)]:
            max = count[ord(i)]
            c = i

    return c
print(getMaxOccuringChar(""))

您可以将max=-1替换为min=lenstr,在第二个for循环中,您可以将if maxcount[ordi]。

即使不能使用min或max函数,也不应该使用min或max作为变量名。这是否回答了您的问题?你问的东西写下来就没有意义了。字符串中字母的出现次数是一个值,而不是可以找到最小值和最大值的范围。要找到的是出现次数的最小值或所有字母频率中的最小频率。您可以用minimum frequency代替minimum,这意味着字符串中可能有多个字母具有该频率。