Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to count and order numbers in a list rachyioli Unladen Swallow Posts: 1 Threads: 1 Joined: Aug 2019 Reputation: 0 Likes received: 0 #1 Aug-21-2019, 10:22 AM (This post was last modified: Aug-22-2019, 03:22 AM by scidam. Edited 2 times in total. Edit Reason: tags added ) Hi guys, I have a list of random numbers and I want to know how many times each number occurs on the list ; I want them ordered by the ones that occurred the most first. This is my program : ```lex=(1,2,3,4,5,6,1,2,3,3,3) x=0 for i in range(7): print(x," appeared ", lex.count(i)) x+=1 ```and this is what I get : ``````Output: 0 is 0 1 is 2 2 is 2 3 is 4 4 is 1 5 is 1 6 is 1 ``````what I need now is to order them from the ones that happened the most in the list: In this case 3 should be at the top because it happened four times ... is there a way I can use two functions at the same time in this loop. thanks scidam wrote Aug-22-2019, 03:22 AM:Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time. buran Posts: 5,212 Threads: 94 Joined: Sep 2016 Reputation: 373 Likes received: 1118 #2 Aug-21-2019, 10:33 AM https://docs.python.org/3.7/library/coll...ns.Counter ```>>> from collections import Counter >>> spam = Counter((1,2,3,4,5,6,1,2,3,3,3)) >>> spam Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1}) >>> ``` perfringo Da Bishop Posts: 1,283 Threads: 5 Joined: Jun 2018 Reputation: 128 Likes received: 282 #3 Aug-21-2019, 10:51 AM (Aug-21-2019, 10:22 AM)rachyioli Wrote: what I need now is to order them from the ones that happened the most in the list You can extend solution provided by buran with .most_common method. If you want regular dict you can convert: ```>>> spam.most_common(len(spam.keys())) [(3, 4), (1, 2), (2, 2), (4, 1), (5, 1), (6, 1)] >>> dict(spam.most_common(len(spam.keys()))) {3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1} ``` 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. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post output a list of random numbers 'x' columns wide adityavpratap 4 112 Jan-13-2020, 05:32 PM Last Post: perfringo How do I name a list while generating numbers on the fly? Pleiades 6 239 Dec-03-2019, 05:47 PM Last Post: jefsummers How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 5 395 Nov-21-2019, 09:02 PM Last Post: Pleiades Subtract 11 from entire list of quoted numbers Pleiades 1 155 Nov-14-2019, 10:26 AM Last Post: Larz60+ list approach due nested order 3Pinter 6 213 Oct-07-2019, 01:49 PM Last Post: 3Pinter List of numbers to add Planeuge 4 308 Sep-21-2019, 10:41 PM Last Post: Planeuge weird result trying to remove numbers from a list Exsul 6 365 Aug-27-2019, 05:10 AM Last Post: perfringo Count elements in a list only if they are following each other and are similar law 10 676 Aug-02-2019, 10:54 PM Last Post: law removing duplicate numbers from a list calonia 12 1,013 Jun-16-2019, 12:09 PM Last Post: DeaD_EyE Count the number of items in a nested list iofhua 2 406 Jun-07-2019, 06:47 PM Last Post: iofhua

Forum Jump:

Users browsing this thread: 1 Guest(s)