Python Forum

Full Version: List of random numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Just because it's Friday:

>>> import random
>>> from collections import Counter
>>> result = dict(Counter(random.choices(range(101), k=1000)).most_common())
(Dec-02-2022, 04:00 PM)astral_travel Wrote: [ -> ]suppose i want to order the output so the order of the numbers will be by their prevalence...and not by the numeric order as it is at the moment...
Don't call dict on the Counter,use the most_common method of Counter.
>>> count = Counter(a)
# All
>>> count.most_common()
....

# 5 most common 
>>> count.most_common(5)
[(58, 17), (11, 16), (99, 16), (13, 15), (63, 15)]

>>> count.most_common(1)
[(58, 17)]
So 58 was the most common number in this run bye the count of 17.

Edit:
posted at same tiime as previous poster.
yea..you all wrote at the same time !

thanks to the three of you !

it's amazing how things can be simplified
(Dec-02-2022, 04:00 PM)astral_travel Wrote: [ -> ]now, suppose i want to order the output so the order of the numbers will be by their prevalence

I've not looked at this in any detail, and I've never used Counter, but if you now have a dictionary and each key value pair holds the number and the occurrence, then I would have thought that you could extract the data and the key, then list them high to low, or low to high, by the occurrence value.

It's maybe more coding than you need, if such a function is already a part of a library, but in the interest of learning, it could prove to be a useful exercise.
Guess you didn't read the part about Counter.most_common() that does exactly what you are looking for.
(Dec-02-2022, 06:31 PM)rob101 Wrote: [ -> ]
(Dec-02-2022, 04:00 PM)astral_travel Wrote: [ -> ]now, suppose i want to order the output so the order of the numbers will be by their prevalence

I've not looked at this in any detail, and I've never used Counter, but if you now have a dictionary and each key value pair holds the number and the occurrence, then I would have thought that you could extract the data and the key, then list them high to low, or low to high, by the occurrence value.

It's maybe more coding than you need, if such a function is already a part of a library, but in the interest of learning, it could prove to be a useful exercise.

yea i just might try that...
(Dec-02-2022, 07:04 PM)deanhystad Wrote: [ -> ]Guess you didn't read the part about Counter.most_common() that does exactly what you are looking for.

OMG i thought what follows beneath are totally other functions...no wonder there was written so little...hehe

just got that these are sub classes of the same function....you're right at that, my fault....

at least i'll be wiser for the next time

hehe
(Dec-02-2022, 07:28 PM)astral_travel Wrote: [ -> ]
(Dec-02-2022, 07:04 PM)deanhystad Wrote: [ -> ]Guess you didn't read the part about Counter.most_common() that does exactly what you are looking for.

OMG i thought what follows beneath are totally other functions...no wonder there was written so little...hehe

just got that these are sub classes of the same function....you're right at that, my fault....

at least i'll be wiser for the next time

hehe
They are methods of the Counter class. Functions do not have sub classes.
Pages: 1 2