[Tutor] Sort a Set

Jacob S. keridee at jayco.net
Thu Sep 1 01:59:36 CEST 2005


How about this?  Not only does it count each element, but you can also get a 
sorted set without using set!

a = [24,24,24,16,16,15,15]
b = {}
for i in a:
    try: b[i] += 1
    except KeyError: b[i] = 1
print b
li = b.keys()
print li
li.sort()
print li
li.reverse()
print li

has output

{24: 3, 16: 4, 15: 2}
[24, 16, 15]
[15, 16, 24]
[24, 16, 15]

Respectfully,
Jacob Schmidt

----- Original Message ----- 
From: "Jonas Melian" <jonasmg at softhome.net>
To: <tutor at python.org>
Sent: Tuesday, August 23, 2005 8:25 AM
Subject: [Tutor] Sort a Set


>I get a list of repeated numbers  [24, 24, 24, 16, 16, 15, 15 ]
> Is possible get it without repeated numbers, without using set()?
>
> If I use set, then the list is unsorted and i cann't sorting it.
>
> For get the values i use:
>
> [x[0] for x in cardTmp]
>
> or:
>
> from itertools import imap
> for i in imap(lambda x: x[0], cardTmp): print i
>
> A idea it would be create a generator that will return elements one by
> one, and then it would be possible know if that element is in the new
> list. But generators are in 2.4
>
>
> Python 2.3.5
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list