The following is the old boy education compiled Python teaching materials, reproduced please indicate the source: http://www.oldboyedu.com
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, so, in set, there is no duplicate key, so it is not too heavy to say that the set is natural, we can now define a set, to see the difference between him and the dictionary.
I. Defining a Collection
s = {1,2,3,4}# This is set d = {' user ': ' Guo ', ' age ': 18}# This is a dictionary
We can convert to collections by existing data types, such as converting a list to a collection
Li =[2,3,4,5,6]s = set (LI) print (s) Run result: {2, 3, 4, 5, 6}
Two. Set increase
You can add elements to the set by using the Add (key) method, and you can add them repeatedly, but without effect:
s = {1,2,3,4}s.add (5) print (s) s.add (5) print (s) Run result: {1, 2, 3, 4, 5}{1, 2, 3, 4, 5}
Three. Collection Delete
You can delete an element by using the Remove (key) method:
s = {1,2,3,4}s.remove (4) # If the element does not exist, the deletion will be error S.discard (4) # If the element does not exist, this deletion will not error s.pop () # Random deletion
Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so two sets can do the intersection and set of mathematical meanings, and so on:
Intersection
Linux = {1,2,3,4,56}python = {4,5,6,98,42,2}print (linux.intersection (python)) print (Linux & python) Run result: {2, 4}{2, 4 }
and set
Linux = {1,2,3,4,56}python = {4,5,6,98,42,2}print (linux.union (python)) print (Linux | python) run result: {1, 2, 3, 4, 98, 5, 6, 42, 56}{1, 2, 3, 4, 98, 5, 6, 42, 56}
Subtraction
linux = {1,2,3,4,56}python = {4,5,6,98,42,2}print (linux.symmetric_difference (python)) Print (Python ^ linux) Run result: {1, 98, 3, 5, 6, 56, 42}{1, 3, 98,  5, 6, 56, 42}
The only difference between set and dict is that it does not store the corresponding value, but the set principle is the same as the dict, so it is also not possible to put mutable objects, because it is not possible to determine whether the two Mutable objects are equal, and there is no guarantee that there will be no duplicate elements inside the set. Try putting the list in set to see if it will give an error.
Four. Immutable objects
As we said above, Str is an immutable object, and list is a mutable object.
For mutable objects, such as list, to manipulate the list, the contents of the list will change, such as:
A = [' C ', ' B ', ' A ']a.sort () print (a) run result: [' A ', ' B ', ' C ']
For non-mutable objects, such as STR, we operate on STR:
A = ' abc ' Print (A.replace (' A ', ' a ')) print (a) operation result: ' ABC ' ABC '
Although the string has a replace () method, it does change the ' ABC ', but the variable A is still ' abc ', how should it be understood?
Let's start by changing the code to the following:
A = ' abc ' b = A.replace (' A ', ' a ') print (b) print (a) Run result: ' abc '
Always keep in mind that a is a variable, and that ' abc ' is a String Object! Sometimes, we often say that the content of object A is ' abc ', but in fact it means that a is itself a variable, it points to the content of the object is ' abc ':
When we call A.replace (' A ', ' a '), the actual calling method replace is on the string object ' abc ', and this method, although named Replace, does not change the contents of the string ' abc '. Instead, the Replace method creates a new string ' abc ' and returns, if we point to the new string with variable B, it's easy to understand that variable a still points to the original string ' abc ', but the variable B points to the new string ' abc ':
Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.
Old boy Python learning data type collection