Find most numbers in the lists
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()
Output
None
It works when I try this path:
>>> lst = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 545, 56, 6, 7, 67]
>>> max(lst,key=lst.count)
4
I want to determine the number that has happened the most times. I'm not sure where I made the mistake for the first def function.
source to share
First you need to parse your input correctly. You can't just use it split(" ")
yourself as one of your entrances had double space.
Second, you don't need a loop, as it max
does the loop for you.
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)
Since you were looping, I assumed that you were trying to deal with the case where multiple numbers may have the same occurrences (ex:) '2 3 40 1 5 4 3 3 9 9 9'
. In this case, the following code will get all the highs:
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)
Here's a more concise version that uses slightly more complex code:
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)
If you want highly efficient code, it makes sense to use 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])
source to share
Usage can use str.split()
to split your string into a list. Then you can use map
to convert strings to integer. A collections.Counter
can be used to count the occurrence of each whole in a relatively efficient manner. Finally, max
with a lambda argument for a key by number of occurrences, it can be used to pull the target value.
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])
Note that no argument str.split
is provided and usage is avoided list.count
(which is rather inefficient in this scenario). If you want to get all target values ββwith the maximum number of occurrences, you can drop that last line and use:
max_occurrences = max(counter.values())
target_values = [k for k, v in counter.items() if v == max_occurrences]
source to share
There are actually a couple of issues with the code currently standing. You seem to be printing the result of a function that returns None
and you are not dividing your string correctly:
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()
source to share
You can just write your code like:
>>> 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'
>>>
so your function should be like this:
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)
source to share