Solved

converting between lists and sets

Posted on 2004-08-29
6
221 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now