473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Which module is "set " in?

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
Jul 18 '05 #1
3 1713
Anthony Liu wrote:
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
Jul 18 '05 #2
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()[i]
#
# 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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/

Jul 18 '05 #3
[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.p...n&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


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
I get this message when I tried to make a .MDE of my database. "Compile error in hidden module: Form_frmPurchaseReq" I have been using this database for several months and just thought I would...
5
by: Bob | last post by:
Are they different names for the same concept ?
5
by: BH | last post by:
Hi what would be the C# equivalent of a VB.NET file that looks like this: Module Utilities Public Sub UtilitySubOne ( ByVal arg As String) // blah blah End Sub
5
by: Studio P.M. | last post by:
Having built successfully my first HTTP Module prototype (as for Q307996 - HOW TO: Create an ASP.NET HTTP Module ...) without really understanding what I've done, now I kindly ask this community...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
by: Shehryar | last post by:
AOA, I urgently want to know the major difference between "Module" and a "Class Module". Plz, reply me as early as possible. Thanx!
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
22
by: ZioMiP | last post by:
Hi to all... I'm actually using Tkinter for my GUI... but I need to "put a piece of a web-page in a widget" how can I do? which GUI module do you suggest me to use for do that? or which GUI...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.