Python Forum
How to count and order numbers in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to count and order numbers in a list
#1
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
Reply
#2
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})
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,435 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Copying the order of another list with identical values gohanhango 7 1,060 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Function to count words in a list up to and including Sam Oldman45 15 6,402 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,256 May-22-2023, 10:39 PM
Last Post: ICanIBB
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,531 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Row Count and coloumn count Yegor123 4 1,267 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,497 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020