First, the collection
1, the definition of the collection
in [74]: s = {}in [74]: s = {} # Empty curly braces are empty dictionaries in [75]: type (s) out[75]: dictin [77]: type (s) out[77]: setin [78]: help (set) Help on class set in module builtins:class set (object) | set () -> new empty set object | set (iterable) - > new set object | | Build an unordered collection of unique elements. | | methods defined Here: in [80]: s = set ([1, 2]) in [81]: sout[81]: {1, 2 }in [82]: s = set ("Xxj") in [83]: sout[83]: {' J ', ' X '}In [84]:  S = {1, 2, 1, 3}IN [85]: SOUT[85]: {1, 2, 3}
collections are unordered, elements cannot be duplicated, elements can be hashed (hash, immutable)
Second, the operation of the collection
1, increase
Z## set.add () In [86]: sout[86]: {1, 2, 3}in [87]: s.add ("a") # add a single element in place, the element to be hashed in [88]: sout[88]: {1, 2, 3, ' a '}in [89]: s.add (3) in [90]: sout[90]: {1, 2, 3, ' a '}in [93]: s.add ([1, &NBSP;2])---------------------------------------------------------------------------typeerror Traceback (most recent call last) <ipython-input-93-2beaf0c16593> in <module> ()----> 1 S.add ([1, 2]) typeerror: unhashable type: ' list ' In [94]: help (S.add) In [95]: s.add ((1, 2)) in [96]: sout[96]: {(1, 2), 1, 2, 3, ' a '}## set.update () # adds an iterative object's element In [99]: help (s.update) Help on built-in function update: Update (...) method of builtins.set instance update a set with the union of itself and others. in [127]: s = set () In [128]: sout[128]: set () In [129]: type (s) Out[129]: set in [101]: s.update (------------------------------------------------) -----------------------typeerror Traceback (most recent call l<ipython-input-101-c184888ad9c5> in <module> ()----> 1 s.update (typeerror: ' int ' object is not iterablein&nbSp [131]: s.update (["a"]) in [132]: sout[132]: {' a '}in [133]: s.update (["A"], [ "B"]) in [134]: sout[134]: {' A ', ' B '}in [135]: s.update (["A"], ["B"], 1 )-----------------------------------------------------------------------typeerror Traceback (Most recent call last) < Ipython-input-135-fc556b8d9726> in <module> ()----> 1 s.update (["A"], ["B" ], 1) typeerror: ' int ' object is not iterablein [136]: s.update (["a"], ["B"], "XJ") in [137]: sout[137]: {' a ', ' B ', ' J ', ' X '}in [139]: s.update ([["S", "B"]])----------------------------------------------------------------------- typeerror Traceback (Most recent call last) < Ipython-input-139-da563f39a191> in <module> ()----> 1 s.update ([["S", "B"] ]) typeerror: unhashable type: ' list '
2. By deleting
## set.remove () in [142]: sout[142]: {' a ', ' B ', ' J ', ' X '}In [143]: s.remove ("a") in [144]: sout[144]: {' B ', ' J ', ' X '}in [151]: s.remove (" S ")-----------------------------------------------------------------------keyerror Traceback (Most recent call last) < Ipython-input-151-332efdd48daa> in <module> ()----> 1 s.remove ("s") KeyError: ' S ' ## set.pop () in [153]: s = {1, 2, 3, 4}in [154]: S.pop () Out[154]: 1In [155]: sOut[155]: {2, 3, 4}In [156]: s.pop (5)-----------------------------------------------------------------------typeerror Traceback (Most recent call last) < Ipython-input-156-23a1c03efc29> in <module> ()----> 1 s.pop (5) TypeError: Pop () takes no arguments (1 given) in [157]: s.pop () out[157]: 2in [158]: s.pop () Out[158]: 3in [159]: s.pop () Out[159]: 4in [160]: s.pop ()----- ------------------------------------------------------------------keyerror Traceback (Most recent call last) < Ipython-input-160-e76f41daca5e> in <module> ()----> 1 s.pop () KeyError: ' Pop from an empty set ' ## set.discard ()In [165]: help (Set.discard) help on method_descriptor:discard (...) remove an element from a set if it is a member. if the element is not a member, do nothing. In [166]: s = {1, 2, 3}in [167]: s.discard (2) In [168]: s.discard (1, 3)------------------------------------ -----------------------------------typeerror traceback (most recent call last) <ipython-input-168-8702b734cbc4> in < Module> ()----> 1 s.discard (1, 3) Typeerror: discard () takes exactly one argument (2 given) In&nbsP [169]: s.discard (2) # element does not exist, will not error in [170]: sout[170]: {1, 3}in [32]: s.clear () In [33]: sout[33]: set () In [47]: del (s) In [48]: S-----------------------------------------------------------------------nameerror Traceback (Most recent call last) < Ipython-input-48-f4d5d0c0671b> in <module> ()----> 1 snameerror: name ' s ' is not defined
Summary:
Remove removes the given element when the element does not exist, throws Keyerror
Discard Delete the given element, the element does not exist, nothing is done
The pop randomly deletes an element and returns it, and the collection is empty to return keyerror,
Clear Empty Collection
3, change
set cannot modify a single element
4. Find
Collections cannot be indexed, collections are not linear, no indexes
Collection does not have a way to access a single element
Collection does not find a method
when doing member operations (in and not), set is much more efficient than List (O (1) and O (n));
O (n) is not necessarily less than O (1) and needs to look at data size
Collection Operations for collections
"Python" 10, Python built-in data structure collection