473,480 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How would you find how many times a number is entered?

Thekid
145 New Member
I'm wondering how you'd enter a lot of numbers and have the script print out the ones that were repeated the most. Something like this:

(numbers entered)
1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23
Print out would be:
1,4,6,23
because they are the most used, if I wanted only the highest 4 numbers used.
Mar 13 '07 #1
10 1680
bartonc
6,596 Recognized Expert Expert
I'm wondering how you'd enter a lot of numbers and have the script print out the ones that were repeated the most. Something like this:

(numbers entered)
1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23
Print out would be:
1,4,6,23
because they are the most used, if I wanted only the highest 4 numbers used.
Expand|Select|Wrap|Line Numbers
  1. l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23]
  2. l.sort()
  3. outList = [] # an empty list
  4. print l
  5. itemCount = 1 # init the number if times this is in the list
  6. for i, item in enumerate(l):
  7.     try:
  8.         j = l.index(item, i + 1) # don't actully need j
  9.         itemCount += 1
  10.     except ValueError:
  11.         result = (itemCount, item)
  12.         print result
  13.         outList.append(result)
  14.         itemCount = 1 # reset the number if times this is in the list
  15. outList.sort(reverse=True)
  16. for outTuple in outList[:4]:
  17.     print outTuple[1],
  18.  
Output:
[1, 1, 3, 4, 4, 6, 6, 6, 8, 10, 12, 14, 19, 20, 23, 23, 23, 24, 33, 34]
(2, 1)
(1, 3)
(2, 4)
(3, 6)
(1, 8)
(1, 10)
(1, 12)
(1, 14)
(1, 19)
(1, 20)
(3, 23)
(1, 24)
(1, 33)
(1, 34)
23 6 4 1
Mar 13 '07 #2
Thekid
145 New Member
Wow! Impressive.....thank you very much!
Mar 13 '07 #3
bartonc
6,596 Recognized Expert Expert
Wow! Impressive.....thank you very much!
You're welcome. It's just a little something that I whipped up while eating my lunch.
Mar 13 '07 #4
ghostdog74
511 Recognized Expert Contributor
another way

Expand|Select|Wrap|Line Numbers
  1. >>> l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23  ]
  2. >>> cnt = [l.count(item) for item in set(l)]
  3. >>> cnt
  4. [2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1]
  5. >>> sorted(zip(cnt,set(l)))[-4:]
  6. [(2, 1), (2, 4), (3, 6), (3, 23)]
  7. >>> for i in sorted(zip(cnt,set(l)))[-4:]:
  8. ...  print i[1],
  9. ...
  10. 1 4 6 23
  11.  
Mar 14 '07 #5
bvdet
2,851 Recognized Expert Moderator Specialist
May I get in on this thread?
Expand|Select|Wrap|Line Numbers
  1. nums = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23]
  2. def top_list(items, top):
  3.     dd = {}
  4.     for item in items:
  5.         if item in dd:
  6.             dd[item]=dd[item]+1
  7.         else:
  8.             dd[item]=1
  9.     itemLst = [(dd[key],key) for key in dd]
  10.     itemLst.sort(lambda a, b: cmp(b, a))
  11.     print itemLst
  12.     for i in range(min(top, len(itemLst))):
  13.         yield itemLst[i][1]
  14.  
  15. for i in top_list(nums, 4):
  16.     print i,
  17.  
  18. '''    
  19. >>>  [(3, 23), (3, 6), (2, 4), (2, 1), (1, 34), (1, 33), (1, 24), (1, 20), (1, 19), (1, 14), (1, 12), (1, 10), (1, 8), (1, 3)]
  20. 23 6 4 1
  21. '''
I am in Python 2.3, therefore the cmp function in sort.
Mar 14 '07 #6
bartonc
6,596 Recognized Expert Expert
Thanks BV and GD. Two great coders show that there are alway many ways to skin a cat in Python. You guys rock!
Mar 14 '07 #7
bartonc
6,596 Recognized Expert Expert
another way

Expand|Select|Wrap|Line Numbers
  1. >>> l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23  ]
  2. >>> cnt = [l.count(item) for item in set(l)]
  3. >>> cnt
  4. [2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1]
  5. >>> sorted(zip(cnt,set(l)))[-4:]
  6. [(2, 1), (2, 4), (3, 6), (3, 23)]
  7. >>> for i in sorted(zip(cnt,set(l)))[-4:]:
  8. ...  print i[1],
  9. ...
  10. 1 4 6 23
  11.  
I should have thought of pulling off the end of the list. Very nice.
Mar 14 '07 #8
Thekid
145 New Member
All very nice! Question though about ghostdog74's.....I get this:

Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
print i[l],
TypeError: tuple indices must be integers

If I change it to simply 'print i' it works but like this:
(2, 1)
(2, 4)
(3, 6)
(2, 23)

This is fine but I was just curious as to why mine wouldn't print out the same way.
Mar 15 '07 #9
bartonc
6,596 Recognized Expert Expert
All very nice! Question though about ghostdog74's.....I get this:

Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
print i[l],
TypeError: tuple indices must be integers

If I change it to simply 'print i' it works but like this:
(2, 1)
(2, 4)
(3, 6)
(2, 23)

This is fine but I was just curious as to why mine wouldn't print out the same way.
... print i[1],
One is definitely an integer. You haven't included your version. Looks like you have a typo.
Mar 16 '07 #10
ghostdog74
511 Recognized Expert Contributor
All very nice! Question though about ghostdog74's.....I get this:

Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
print i[l],
TypeError: tuple indices must be integers

If I change it to simply 'print i' it works but like this:
(2, 1)
(2, 4)
(3, 6)
(2, 23)

This is fine but I was just curious as to why mine wouldn't print out the same way.
as barton said, its a 1, not an 'l' . As you can see the results are tuples.
Mar 16 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3819
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
4
8317
by: Bite My Bubbles | last post by:
How can i count the number of times a string appears within another string. Thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!
5
1329
by: Don Sealer | last post by:
I have a database that I need to track certain measurements numerous times per day. I need to record several aspects like, length, width, thickness, plus a few other things. I've been recording...
1
2138
by: Diane Yocom | last post by:
I'm still very new to ASP.Net, so wanted to get some advice on how to solve the following design problem (sorry my explanation is so long): I'm developing an ASP.Net intranet app (using VB.Net...
5
1458
by: zaie | last post by:
The files I'm referring to can be found here: http://www.moxybox.com/programming After going through this in debugger numerous times, I can't figure out why this code continues to return false:...
6
3699
by: reon | last post by:
Here is my source code.... And any one pls help me how can we find the output integers sort by ascending and find there average... #include<iostream.h> #include<conio.h> void main() { int...
2
1337
by: antonyfran | last post by:
hey can some please tell me how to run this class a specific number of times. maybe by using a for loop?. i am making a game called memory game where you match the cards which are the same. i want...
2
2562
by: raphael001 | last post by:
In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values...
12
3510
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.