Python learning notes (10) Python collection (2)

Posted by insub2 on Fri, 03 Apr 2020 21:07:50 +0200

Common methods of collection

add() adds an element to the collection. If the element already exists in the collection, the method will fail

 1 >>> help(set.add)
 2 Help on method_descriptor:
 3 
 4 add(...)
 5     Add an element to a set.   #Add an element to the collection
 6 
 7     This has no effect if the element is already present. #If an element already exists in the collection, this method is invalid
 8 
 9 >>> a ={"baidu","google"}
10 >>> type(a)
11 <type 'set'>
12 >>> a.add("weibo") #Add elements to collection a
13 >>> a
14 set(['baidu', 'weibo', 'google'])
15 >>> id(a)  #Address of set a in memory
16 64656104L
17 >>> a.add("ali") #Add elements to collection a
18 >>> a
19 set(['baidu', 'weibo', 'google', 'ali'])
20 >>> id(a) #The memory address in set a has not changed. It is modified in place. It is a variable set
21 64656104L
22 >>> a.add("google") #If the added element exists in the collection, nothing will be done
23 >>> b ={} #Create an empty collection
24 >>> b.add("python")
25 Traceback (most recent call last):
26   File "<stdin>", line 1, in <module>
27 AttributeError: 'dict' object has no attribute 'add' #The error message is that there is no add function in the dictionary
28 >>> type(b)  #b is a dictionary
29 <type 'dict'>
30 >>> b =set() #Create an empty collection
31 >>> type(b)
32 <type 'set'>
33 >>> b.add("python") #Add an element to b
34 >>> b
35 set(['python'])
36 >>> b.add([1,2,3]) #Add a list to b. the error reporting list cannot be hash ed and can be changed
37 Traceback (most recent call last):
38   File "<stdin>", line 1, in <module>
39 TypeError: unhashable type: 'list'
40 >>> b.add((1,2,3)) #You can add an element to a collection
41 >>> b
42 set(['python', (1, 2, 3)])
43 >>>

update() update

 1 >>> help(set.update)
 2 Help on method_descriptor:
 3 
 4 update(...)
 5     Update a set with the union of itself and others. #Update a set, and convert the contents of the set itself and other parameters into a set
 6 
 7 >>> a
 8 set(['baidu', 'weibo', 'google', 'ali'])
 9 >>> b
10 set(['python', (1, 2, 3)])
11 >>> a.update(b) #Update set b to set a
12 >>> a
13 set(['baidu', 'weibo', 'google', 'ali', 'python', (1, 2, 3)])
14 >>> a.update("test")  #Update a string to set a
15 >>> a
16 set(['baidu', 'weibo', 's', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])
17 >>>

pop() randomly removes an element from the collection, and takes this element as the return value. The pop function has no parameters and cannot specify an element

 1 >>> help(set.pop)
 2 Help on method_descriptor:
 3 
 4 pop(...)
 5     Remove and return an arbitrary set element. #Remove an element from the collection and return it
 6     Raises KeyError if the set is empty. #If this set is empty, an error keyError will be reported
 7 
 8 >>> b
 9 set(['python', (1, 2, 3)])
10 >>> b.pop()
11 'python'
12 >>> b
13 set([(1, 2, 3)])
14 >>>

remove() deletes the specified element from the collection. The deleted element must be a member of the collection. If not, an error KeyError will be reported

 1 >>> help(set.remove)
 2 Help on method_descriptor:
 3 
 4 remove(...)
 5     Remove an element from a set; it must be a member. #Delete the specified element from the collection. The deleted element must be a member of the collection
 6 
 7     If the element is not a member, raise a KeyError. #KeyError is reported if it is not an element in the collection
 8 
 9 >>> a
10 set(['baidu', 'weibo', 's', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])
11 >>> a.remove("s")
12 >>> a
13 set(['baidu', 'weibo', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])
14 >>> a.remove("s")
15 Traceback (most recent call last):
16   File "<stdin>", line 1, in <module>
17 KeyError: 's'
18 >>>

discard() deletes the specified element from the collection. The deleted element must be a member of the collection. If not, no action will be taken

Similar to remove(), the difference is that if remove() deletes elements that are not in the collection, an error will be reported. If discard() deletes elements that are not in the collection, no error will be reported.

Example:

 1 >>> help(set.discard)
 2 Help on method_descriptor:
 3 
 4 discard(...)
 5     Remove an element from a set if it is a member.
 6 
 7     If the element is not a member, do nothing.
 8 
 9 >>> a.discard("s")
10 >>>

clear() deletes all elements in the collection

 1 >>> help(set.clear)
 2 Help on method_descriptor:
 3 
 4 clear(...)
 5     Remove all elements from this set.
 6 
 7 >>> a.clear()
 8 >>> a  #Set is an empty set
 9 set([])
10 >>>

Topics: Python Google Attribute