0

I want to use sets as data structure. how should i make it possible? what are the commands I need?

Like closedset = set()

is this ok?

And in a set, if i want to get some value out, what is the command for that one?

asked Jul 16, 2010 at 8:50
1

2 Answers 2

4

Correct. To create an empty set, write foo = set(). To retrieve values, you can iterate over the set:

for val in someset:
 print val

You can also write val in someset to check if an item is in a set.

Be sure to read the documentation for how to do set operations.

answered Jul 16, 2010 at 8:55
Sign up to request clarification or add additional context in comments.

Comments

1

You can saymySet = set(), which will give you a blank set to work with. Additionally, one method I found useful usually is converting from tuples/lists to sets, which you can do by saying mySet = set([1,2,3,4,5]).

What do you mean you'd like to get some value out? As in whether or not an item is a member of the set? For determining membership, you can use the usual python idiom of 1 in mySet to determine whether 1 is in the set or not.

And yes, the docs are right here

answered Jul 16, 2010 at 8:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.