473,426 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

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

Thekid
145 100+
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 1679
bartonc
6,596 Expert 4TB
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 100+
Wow! Impressive.....thank you very much!
Mar 13 '07 #3
bartonc
6,596 Expert 4TB
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 Expert 256MB
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 Expert Mod 2GB
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 Expert 4TB
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 Expert 4TB
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 100+
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 Expert 4TB
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 Expert 256MB
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
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.