Sets in Python

Moshe Zadka moshez at zadka.site.co.il
Tue Jan 30 13:52:07 EST 2001


On Tue, 30 Jan 2001, "Magnus Lie Hetland" <mlh at idi.ntnu.no> wrote:

> And if one was to write a small wrapper of some kind --
> is there any way to implement "x in s" without a linear search?
> Or does the "in" operator always loop from 0 and up?
> (It would certainly be nice to be able to override it...)

It can be if you're using Python 1.6 or later:

class set:

	def __init__(self, els):
		self.data = {}
		for el in els:
			self.data[el] = None

	def __contains__(self, el):
		return self.data.has_key(el)

1 in set([1, 2])
3 not in set([1, 2])
-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!
Fingerprint: 4BD1 7705 EEC0 260A 7F21  4817 C7FC A636 46D0 1BD6




More information about the Python-list mailing list