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.
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.
- l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23]
-
l.sort()
-
outList = [] # an empty list
-
print l
-
itemCount = 1 # init the number if times this is in the list
-
for i, item in enumerate(l):
-
try:
-
j = l.index(item, i + 1) # don't actully need j
-
itemCount += 1
-
except ValueError:
-
result = (itemCount, item)
-
print result
-
outList.append(result)
-
itemCount = 1 # reset the number if times this is in the list
-
outList.sort(reverse=True)
-
for outTuple in outList[:4]:
-
print outTuple[1],
-
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
Wow! Impressive.....thank you very much!
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.
another way -
>>> l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23 ]
-
>>> cnt = [l.count(item) for item in set(l)]
-
>>> cnt
-
[2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1]
-
>>> sorted(zip(cnt,set(l)))[-4:]
-
[(2, 1), (2, 4), (3, 6), (3, 23)]
-
>>> for i in sorted(zip(cnt,set(l)))[-4:]:
-
... print i[1],
-
...
-
1 4 6 23
-
bvdet 2,851
Recognized Expert Moderator Specialist
May I get in on this thread? - nums = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23]
-
def top_list(items, top):
-
dd = {}
-
for item in items:
-
if item in dd:
-
dd[item]=dd[item]+1
-
else:
-
dd[item]=1
-
itemLst = [(dd[key],key) for key in dd]
-
itemLst.sort(lambda a, b: cmp(b, a))
-
print itemLst
-
for i in range(min(top, len(itemLst))):
-
yield itemLst[i][1]
-
-
for i in top_list(nums, 4):
-
print i,
-
-
'''
-
>>> [(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)]
-
23 6 4 1
-
'''
I am in Python 2.3, therefore the cmp function in sort.
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!
bartonc 6,596
Recognized Expert Expert
another way -
>>> l = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23 ]
-
>>> cnt = [l.count(item) for item in set(l)]
-
>>> cnt
-
[2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1]
-
>>> sorted(zip(cnt,set(l)))[-4:]
-
[(2, 1), (2, 4), (3, 6), (3, 23)]
-
>>> for i in sorted(zip(cnt,set(l)))[-4:]:
-
... print i[1],
-
...
-
1 4 6 23
-
I should have thought of pulling off the end of the list. Very nice.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: Bite My Bubbles |
last post by:
How can i count the number of times a string appears within another string.
Thanks a
lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!
|
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...
|
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...
|
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:...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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 ...
| |