Blog Address: http://www.cnblogs.com/yudanqu/
First of all, a brief introduction to Set,set is what we learned in high school, when the nature of the collection includes a point where the collection cannot have duplicate numbers. We now use a more extensive collection, which can store characters and so on.
- Set: Similar to Dict, is a set of keys that does not store value
- Nature: A collection of unordered and non-repeating elements
Perhaps I said that he is similar to Dict, for the first-class students do not understand, how can this be similar? Let's see:
If there is anything to find, the dictionary (dict) and set (set) are expressed in curly braces, and that is the essence of him, so look down first.
1. Create
(1) Create set requires a list or tuple or dict as the input set
(2) Where duplicate elements are automatically filtered in set
2. Add
S is a collection to which elements are added:
- S.add (6) # Add a number 6 to it, and no effect if you add a repetition
- S.add ([7,8,9]) # error because the list cannot be a mutable object as Key,list and Dict
- # tuples can be added as objects because tuples are immutable objects
- S.update () # can split the list tuple into set, including the string, and it will be opened to the collection
3. Delete
- S.remove () # parameter for element inside, cannot be deleted by subscript, because itself unordered, no index
- Set has no index, but can traverse output
- It can also be used for Index,data in enumeration (s), which may appear to be indexed but not valid
4. Intersection
1 a1 = s1 & S2 # to intersect and output new set
5, and set
1 # the assembly returns the new set
The *set itself is not commonly used, but can be used by his type conversion, using its non-repeating characteristics
Yu Tanku (yudanqu)
Blog Address: http://www.cnblogs.com/yudanqu/
Python data type--set (collection)