[Tutor] Sort a Set
kent37 at tds.net
Tue Aug 23 15:39:13 CEST 2005
Jonas Melian wrote:
> 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.
> 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
No, generators and iterators (for which generators are a shortcut) were introduced in Python 2.2, though to use a generator in 2.2 you must include
from __future__ import generators
in your code. Generator *expressions* - which create an iterator instead of a list - are new with 2.4.
Here is a simple generator that returns a single element from each run of items in a sequence; this works in Python 2.3.5:
>>> def unique(seq):
... last = object() # unique marker for no last object
... for item in seq:
... if item != last:
... yield item
... last = item
>>> nums = [24, 24, 24, 16, 16, 15, 15 ]
[24, 16, 15]
More information about the Tutor