0
Write a Python function that takes as input a list of integers returns the most occuring and least occuring integers.
For instance>>> frequency([13,12,11,13,14,13,7,11,13,14,12]) Should return ([7], [13]) >>>frequency([13,12,11,13,14,13,7,11,13,14,12,14,14]) Should return ([7], [13, 14])
16 Respostas
+ 1
show your code to see that you atleast tried
+ 1
sounds like homework :)
+ 1
š
+ 1
Sourav A.S sorry my brother broke his leg, but hes alright now il check when i wake up
+ 1
Cat Sauce geez, thats something else. good luck to you and your brother. happy to hear heās is sort of alright now.
+ 1
Cat Sauce Thanks alot. I hope
your brother is ok.
0
Sourav A.S if you can show us a try you made but failed, il be happy to help you :)
0
def frequency (i):
d=[ ]
d.append(( max(set(i), key=i.count)))
print(d)
0
It is only for getting most occuring integer, but it doesnt
Work if there are multiple integers occuring most
0
so you want to check how many times all the integers have occured?
0
I want know which integers are leat occuring and which are most occuring
0
For eg:
Fun([1,2,1,3,2])
Should return [3,[1,2]]
Where 3 is the least occuring and 1,2 are the most occuring
0
wait 1 min and il help
0
Cat Sauce did you get the code
0
I'm sure I've seen something similar been done on here before, Look up Counter in collections module in python.org.
0
I think this does exactly what you need.
from collections import Counter
mylist = [15, 20, 33, 4, 15, 20, 4, 20, 2, 3, 4]
mylist2 = []
for x in mylist:
mylist2.append(str(x)) # had to do the (str), something about the Count function needing hashable.
def get_most_least(x):
most_occuring = []
least_occuring = []
combined_list = []
cnt = Counter(x)
for a, b in cnt.items():
if b == max(cnt.values()):
most_occuring.append(a)
if b == min(cnt.values()):
least_occuring.append(a)
combined_list = [least_occuring, most_occuring]
return combined_list
print(get_most_least(mylist2))