Python learning notes (10) Python collection (2), python learning notes

Source: Internet
Author: User

Python learning notes (10) Python collection (2), python learning notes

Common methods of collection

Add () adds an element to the set. If this element already exists in the Set, this method will become invalid.

1 >>> help (set. add) 2 Help on method_descriptor: 3 4 add (...) 5 Add an element to a set. # Add an element 6 7 This has no effect if the element is already present to the set. # If an element already exists in the collection, this method will not work 8 9 >>>> a = {"baidu", "google"} 10 >>> type () 11 <type 'set'> 12 >>>. add ("weibo") # add element 13 to set a> a14 set (['baidu', 'weibo ', 'Google']) 15 >>> id (a) # address of set a in memory 16 64656213l17 >>>. add ("ali") # add element 18 to set a> a19 set (['baidu', 'weibo ', 'Google', 'Ali ']) 20 >>> id (a) # The memory address in Set a has not changed. It is modified in situ, and it is a variable set of 21 64656213l22 >>>. add ("google") # If the added element exists in the collection, no operation is performed 23 >>> B ={}# create an empty set 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 function 28 is not added to the dictionary. >>> type (B) # B is a dictionary 29 <type 'dict '> 30 >>> B = set () # create an empty set 31 >>> type (B) 32 <type 'set'> 33 >>> B. add ("python") # add an element to B 34 >>> b35 set (['python']) 36 >>> B. add ([1, 2, 3]) # add a list to B. The error list cannot be hashed and can be changed to 37 Traceback (most recent call last ): 38 File "<stdin>", line 1, in <module> 39 TypeError: unhashable type: 'LIST' 40 >>> B. add (, 3) # You can add an element to the set 41 >>> b42 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 content in the set itself and other parameters to the set 6 7 >>> a 8 set (['baidu', 'weibo ', 'Google ', 'ali']) 9 >>> b10 set (['python', (1, 2, 3)]) 11 >>>. update (B) # update set B to set a 12 >>> a13 set (['baidu', 'weibo ', 'Google', 'Ali ', 'python', (1, 2, 3)]) 14 >>>. update ("test") # update a string to set a 15 >>>> a16 set (['baidu', 'weibo ', 's', 'Google ', 'E', 't', 'ali', 'python', (1, 2, 3)]) 17 >>>

Pop () deletes an element randomly from the set and uses 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 set and return the element 6 Raises KeyError if the set is empty. # If this set is empty, the system reports the keyError 7 8 >>> B 9 set (['python', (1, 2, 3)]) 10 >>> B. pop () 11 'python' 12 >>> b13 set ([(1, 2, 3)]) 14 >>>

Remove () deletes a specified element from the set. The deleted element must be a member of the set. If not, the system reports a KeyError.

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 a specified element from the set. the deleted element must be a member of the set. 6 7. If the element is not a member, raise a KeyError. # If it is not an element in the set, the system will report the KeyError 8 9 >>> a10 set (['baidu', 'weibo ', 's', 'Google', 'E ', 'T', 'ali', 'python', (1, 2, 3)]) 11>. remove ("s") 12 >>> a13 set (['baidu', 'weibo ', 'Google', 'E', 't', 'Ali ', 'python', (1, 2, 3)]) 14 >>>. remove ("s") 15 Traceback (most recent call last): 16 File "<stdin>", line 1, in <module> 17 KeyError: 's' 18 >>>

Discard () deletes a specified element from the set. The deleted element must be a member of the set. If not, no operation is performed.

Similar to remove (), the difference is that removing () is not an element in a set, and an error is returned. If discard () is not an element in the Set, no error is returned.

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 set.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.