Solved

converting between lists and sets

Posted on 2004-08-29
6
231 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
  • 3
  • 3
6 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 50 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

856 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