Solved

converting between lists and sets

Posted on 2004-08-29
6
233 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 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
[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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…
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…

691 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