在列表中查找大多数数字

时间:2017-05-26 02:29:03

标签: python python-3.x

def print_most_numbers_occurrences(numbers_str):
    number_list = list(numbers_str)
    for i in number_list:
        i=max(number_list,key=number_list.count)
    print(i)

def test_print_most_numbers_occurrences():
    print_most_numbers_occurrences('2 3 40 1 5 4 3 3 9  9')
    print_most_numbers_occurrences('9 30 3 9 3 1 4')
    print_most_numbers_occurrences('19 30 13 4 9 3 1 4')


def main():
    print(test_print_most_numbers_occurrences())


main()

输出

None

当我尝试这种方式时它会起作用:

>>> lst = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 545, 56, 6, 7, 67]
>>> max(lst,key=lst.count)
4

我想确定发生次数最多的号码。我不确定我在第一次def功能上做错了什么。

5 个答案:

答案 0 :(得分:3)

首先需要正确解析输入。由于您的某个输入具有双倍空格,因此您无法单独使用split(" ")

其次,您不需要循环,因为max为您循环。

def print_most_numbers_occurrences(numbers_str):
    number_list = [int(x) for x in numbers_str.split()]
    i=max(number_list,key=number_list.count)
    print(i)

由于你在循环,我冒昧地假设你正在尝试处理多个数字可能具有相同出现率的情况(例如:'2 3 40 1 5 4 3 3 9 9 9')。在这种情况下,以下代码将获得所有最大值:

def print_most_numbers_occurrences(numbers_str):
    print(numbers_str.split(" "))
    number_list = [int(x) for x in numbers_str.split()]
    most_occurances = []
    for i in set(number_list):
        occurances = number_list.count(i)
        if len(most_occurances) == 0:
            most_occurances.append(i)
        elif most_occurances[0] < occurances:
            most_occurances = [i]
        elif most_occurances[0] == occurances:
            most_occurances.append(i)
    print(most_occurances)

这是一个更简洁的版本,使用稍微复杂的代码:

def print_most_numbers_occurrences(numbers_str):
    number_list = [int(x) for x in numbers_str.split()]
    result = {i : number_list.count(i) for i in set(number_list)}
    highest = max(result.values())
    most_occurances = [k for k, v in result.items() if v == highest]
    print(most_occurances)

如果您需要高效的代码,最好使用collections Counter

from collections import Counter

def print_most_numbers_occurrences(numbers_str):
    number_list = [int(x) for x in numbers_str.split()]
    result = Counter(number_list)
    highest = max(result.values())
    most_occurances = [k for k, v in result.items() if v == highest]
    print(most_occurances if len(most_occurances) > 1 else most_occurances[0])

答案 1 :(得分:2)

使用.split(&#39;&#39;)代替list()

string = '9 30 3 9 3 1 4'
lst = map(int, filter(None, string.split(' ')))
print(max(lst, key = lambda x: lst.count(x)))
# returns 9

答案 2 :(得分:1)

使用可以使用-lpthread将字符串分解为列表。然后,您可以使用str.split()将字符串转换为整数。可以使用map以相对有效的方式计算每个整数的出现次数。最后,带有lambda参数的collections.Counter可以用来键出出现次数。

max

请注意,string_list = '2 3 40 1 5 4 3 3 9 9'.split() int_list = map(int, string_list) from collections import Counter counter = Counter(int_list) target_value, occurrence_count = max(counter.items(), key=lambda t: t[1]) 没有提供任何参数,并且避免使用str.split(在这种情况下效率非常低)。如果要获得具有最大出现次数的所有目标值,可以放弃最后一行并使用:

list.count

答案 3 :(得分:0)

目前代码中存在一些问题。您似乎正在打印返回None的函数的结果,并且您没有正确地拆分字符串:

def print_most_numbers_occurrences(numbers_str):
    number_list = [int(number) for number in numbers_str.split(' ') if number != ""]
    for i in number_list:
        i=max(number_list,key=number_list.count)
    print(i, number_list.count(i))

def test_print_most_numbers_occurrences():
    print_most_numbers_occurrences('2 3 40 1 5 4 3 3 9  9')
    print_most_numbers_occurrences('9 30 3 9 3 1 4')
    print_most_numbers_occurrences('19 30 13 4 9 3 1 4')


def main():
    test_print_most_numbers_occurrences()


main()

答案 4 :(得分:0)

您只需将代码编写为:

>>> import re
>>> x
'2     3   40    1  5 4  3  3 9  9'
>>> list = re.sub(' +', ' ', x).split(' ')
>>> list
['2', '3', '40', '1', '5', '4', '3', '3', '9', '9']
>>> max(list, key=list.count)
'3'
>>> x = '19 30 13 4 9 3 1 4'
>>> list = re.sub(' +', ' ', x).split(' ')
>>> list
['19', '30', '13', '4', '9', '3', '1', '4']
>>> max(list, key=list.count)
'4'
>>> 

所以你的功能应该是:

def fun(num_string):
    num_list = re.sub(' +', ' ', num_string).split(' ')
    print(max(num_list, key=num_list.count))  # you can write print but I prefer you should use return max(num_list, key=num_list.count)