Which module is "set " in?

A

Anthony Liu

I want to use the set function like

mylist = ['a', 'b', 'b', 'd', 'e', 'a']
myset = set (mylist)

But I don't know what to import, I tried sys, sets,
they don't work.

What's the easy way to find out the module that
contains a particular function?



__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
 
S

Steven Bethard

Anthony said:
I want to use the set function like

mylist = ['a', 'b', 'b', 'd', 'e', 'a']
myset = set (mylist)

But I don't know what to import, I tried sys, sets,
they don't work.

If you're using Python 2.4, they're builtin. If you're using Python
2.3, you'll probably want to do something like:
from sets import Set as set
Python's before 2.3 do not have a set type.
What's the easy way to find out the module that
contains a particular function?

Perhaps the documentation index?

http://docs.python.org/lib/genindex.html

Steve
 
M

Michael Hartl

It's good that you're using Python 2.3, which does have sets available,
as a previous poster mentioned. Users of Python 2.2 or earlier can get
most of the Set functionality using the following class (from Peter
Norvig's utils.py file):

# class Set:
# """This implements the Set class from PEP 218, except it does not
# overload the infix operators.
# Ex: s = Set([1,2,3]); 1 in s ==> True; 4 in s ==> False
# s.add(4); 4 in s ==> True; len(s) ==> 4
# s.discard(999); s.remove(4); 4 in s ==> False
# s2 = Set([3,4,5]); s.union(s2) ==> Set([1,2,3,4,5])
# s.intersection(s2) ==> Set([3])
# Set([1,2,3]) ==> Set([3,2,1]); repr(s) ==> '{1, 2, 3}'
# for e in s: pass"""
#
# def __init__(self, elements=[]):
# self.dict = {}
# for e in elements:
# self.dict[e] = 1
#
# def __contains__(self, element):
# return element in self.dict
#
# def __getitem__(self, i):
# return self.dict.items()
#
# def add(self, element):
# self.dict[element] = 1
#
# def remove(self, element):
# del self.dict[element]
#
# def discard(self, element):
# if element in self.dict:
# del self.dict[element]
#
# def pop(self):
# key, val = self.dict.popitem()
# return key
#
# def clear(self):
# self.dict.clear()
#
# def union(self, other):
# return Set(self).union_update(other)
#
# def intersection(self, other):
# return Set(self).intersection_update(other)
#
# def union_update(self, other):
# for e in other:
# self.add(e)
# return self
#
# def intersection_update(self, other):
# for e in self.dict.keys():
# if e not in other:
# self.remove(e)
# return self
#
# def issubset(self, other):
# for e in self.dict.keys():
# if e not in other:
# return False
# return True
#
# def __iter__(self):
# for e in self.dict:
# yield e
#
# def __len__(self):
# return len(self.dict)
#
# def __cmp__(self, other):
# if self is other: return False
# if not isinstance(other, Set): return id(self) - id(other)
# return cmp(self.dict, other.dict)
#
# def __repr__(self):
# return "{%s}" % ", ".join([str(e) for e in self.dict.keys()])

Michael
 
R

Raymond Hettinger

[Michael Hartl]
It's good that you're using Python 2.3, which does have sets available,
as a previous poster mentioned. Users of Python 2.2 or earlier can get
most of the Set functionality using the following class (from Peter
Norvig's utils.py file):

Py2.3's set module also works under Py2.2. Get a copy from viewcvs:

http://tinyurl.com/6lqr3

or


http://cvs.sourceforge.net/viewcvs..../sets.py?content-type=text/plain&rev=1.43.4.2

Of course, if upgrading to Py2.4 is an option, that is the way to go. If you're
living in a pre 2.2 world without iterators and generators, you're really
missing out.


Raymond Hettinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,221
Messages
2,571,133
Members
47,748
Latest member
LyleMondra

Latest Threads

Top