?
Solved

converting between lists and sets

Posted on 2004-08-29
6
Medium Priority
?
235 Views
Last Modified: 2010-04-16
hi everyone,

i need to get the values from a list and convert make a set, does anyone know how I can get this done, or how I can add to an existing set....much like appending to a list

Also can someone please explain BaseSet to me?


all help welcomed

thanks
jom20
0
Comment
Question by:jom20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 200 total points
ID: 11926165
Have you looked at the documentation of the sets module? It answers your questions.

To make a set from a list:
Under Examples:
>>> from sets import Set
>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
>>> engineers.add('Marvin')                                  # add element

Paragraph 3 of the sets main page includes:
BaseSet [is] an abstract class useful for determining whether something is a set: isinstance(obj, BaseSet).
0
 

Author Comment

by:jom20
ID: 11926179
ok...got it....I did solve the BaseSet  before I needed to create two new sets to use to function thats why I got the BaseSet error. I never saw the conversion I thought there would have been a separate function for that just like in the mutable set class.....anyway thanks very much for your help
0
 

Author Comment

by:jom20
ID: 11926252
hi,

the function add is not part of the set class and as such cannot be called.....do you know why this is?? maybe its included in a later version of the sets class
0
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
LVL 17

Expert Comment

by:ramrom
ID: 11926642
Sets were added in 2.3. My help referes to 2.3.3, and add exists there.
0
 

Author Comment

by:jom20
ID: 11926662
its called __add___ and it adds one set to another which is ok as long as I call each individual elements of the list and then make a set from it...the only problem is I keep getting an error that says ___add___ is not part of Set...just to check if I was to call the method this would be the way to do it right?

ww = Set('f')
fd  =  Set('a')

ww.__add__(fd)

0
 
LVL 17

Expert Comment

by:ramrom
ID: 11926950
Try
>>> help('sets')
... lots of stuff ...
    class Set(BaseSet)
...
... then the "magic" methods __xx__
... __add__ is not there
... then the other methods (including add)

so www.add(fd) should work.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question