Posts: 1,950
Threads: 8
Joined: Jun 2018
Just because it's Friday:
>>> import random
>>> from collections import Counter
>>> result = dict(Counter(random.choices(range(101), k=1000)).most_common())
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 7,313
Threads: 123
Joined: Sep 2016
Dec-02-2022, 06:21 PM
(This post was last modified: Dec-02-2022, 06:22 PM by snippsat.)
(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.
Posts: 230
Threads: 39
Joined: Mar 2020
yea..you all wrote at the same time !
thanks to the three of you !
it's amazing how things can be simplified
Posts: 453
Threads: 16
Joined: Jun 2022
(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.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 6,779
Threads: 20
Joined: Feb 2020
Guess you didn't read the part about Counter.most_common() that does exactly what you are looking for.
Posts: 230
Threads: 39
Joined: Mar 2020
(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...
Posts: 230
Threads: 39
Joined: Mar 2020
(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
Posts: 6,779
Threads: 20
Joined: Feb 2020
(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.
|