Interface of the set classes

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Fri Oct 29 13:26:12 EDT 2004


Steven Bethard a écrit :
> Pierre Barbier de Reuille <pierre.barbier <at> cirad.fr> writes:
> 
>>For me, sets are first sequences. I use set where lists are inefficient 
>>or inadapted, for example when I need to remove redundancy and that I 
>>don't care about the positions of my elements.
> 
> 
> If you still need a fixed ordering after removing duplicates, why don't you just
> revert to a list?

I don't _need_ __getitem__ for itself. I need it because I don't know 
how to iterate over a list and store the current position for future 
use. If I really need to access a given element by position, obviously, 
set would not be the right container. But the sets as they are 
implemented now only allows simple traversal, and that's not acceptable 
for lots of algorithms (or at the cost of complexity and complexity in 
Python becomes quickly critical because iterating is a slow operation).

> 
> 
>>>>l = [11, 1, 2, 2, 3, 5, 11, 3, 7, 5]
>>>>list(set(l))
> 
> [1, 2, 3, 5, 7, 11]
> 
>>>>d = list(set(l))
>>>>d[3]
> 
> 5
> 
> Of course, if you want to maintain the order that items are inserted, you can
> easily do this manually:
> 
> 
>>>>class orderedset(set):
> 
> ... 	def __init__(self, seq):
> ... 		super(set, self).__init__()
> ... 		self._items = []
> ... 		for item in seq:
> ... 			self.add(item)
> ... 	def add(self, item):
> ... 		if item not in self:
> ... 			self._items.append(item)
> ... 		set.add(self, item)
> ... 	def __iter__(self):
> ... 		return iter(self._items)
> ... 	def __getitem__(self, i):
> ... 		return self._items[i]
> ... 
> 
>>>>list(orderedset([11, 1, 2, 2, 3, 5, 11, 3, 7, 5]))
> 
> [11, 1, 2, 3, 5, 7]
> 
>>>>orderedset([11, 1, 2, 2, 3, 5, 11, 3, 7, 5])[2:4]
> 
> [2, 3]
> 
> And, while you're writing your own class to do this, you can change your method
> names to append, etc. if you like.  =)
> 
> The point here, I guess, is that the uses you intend for sets probably do not
> align so well with the uses everyone else has for sets.  Personally, I don't
> expect any ordering in a set, and if I need such ordering I convert back to a
> list.  I also almost never use the lst[x] syntax, preferring in general to just
> iterate with a for-loop (or list comprehension, or generator expression...)  If
> you find you do need the lst[x] syntax all the time, first, I'd double-check to
> see if I couldn't write things better with a for-loop, and then, if I was still
> pretty sure I couldn't, I'd write my orderedset class as above...
> 
> Steve
> 

I agree, I can always create my own structure (and that's what I did in 
exporting the STL set and hash_set) but I feel it's a shame to do so 
when the builtin structure has all the needed characteristic, but my 
algorithm cannot use it without a complete rewritting (and debugging ...)

Pierre



More information about the Python-list mailing list