Solved

converting between lists and sets

Posted on 2004-08-29
6
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PYTHON: Updating local variable in .TPL file 24 140
For Loop in Python - Logic Question 6 87
how to check case insensitive substring 5 53
Python On Mouseover Save URL/Link 4 59
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

920 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

14 Experts available now in Live!

Get 1:1 Help Now