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?
-
9Have you considered to read the [ documentation ](docs.python.org/library/stdtypes.html#set-types-set-frozenset)? It is very important that you learn to work with the Python documentation, it will help you a lot.Felix Kling– Felix Kling2010年07月16日 08:54:42 +00:00Commented Jul 16, 2010 at 8:54
2 Answers 2
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.
Comments
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