Python Study notes third week

Source: Internet
Author: User

First, the basic concept

1. Collection:

A collection is an unordered, non-repeating data tenant that functions:

1, to repeat, a list into a set, it will automatically go to the weight

2, the relationship test, test the two sets of data between the intersection, difference sets, and set of relations

Method of collection:

A, set collection

>>> list_1 = [1,3,5,6,77,3,12]   # list 1>>> list_2 = [1,3,88,4,33,77]  #  List 2>>> list_1 = set (list_1)  # You can change the list to a collection by using the Set method >>> list_1{ 1, 3, 5, 6, A, i}  # collection list_1 now go to weight >>> list_2 = Set (list_2)> >> list_2{1, 3, 4, +,#打印list_1类型, found as set type, proved as set

{1, 3, 5, 6, <class ' Set ' >

    

B, take intersection intersection (list_1 list_2 common elements)

>>> list_1.intersection (list_2)     {1, 3, +}print(list_1  & List_ 2) {1, 3, +}# Both methods can remove the intersection of list_1 and List_2

C, take union (List_1 and list_2 all to the heavy elements)

>>> list_1.union (list_2)                  {1, 3, 4, 5, 6, A, and}>>> list_1  |  list_2{1, 3, 4, 5, 6, three, five}# The above two methods take all the elements of two sets

D, take the difference set (the difference is a set has, but another set does not have elements, so the difference can get two different results)

    

# list_1 exists but does not exist in the list_2 {5, 6} # #list_2中存在但是list_1中不存在 {88, 33, 4}

>>> list_1-list_2
{12, 5, 6}
>>> list_2-list_1
{88, 33, 4}

E, determine the subset (determine whether a collection is in another collection)

>>> list_3 = Set ([1,3])>>> list_3.issubset (list_2) True>>> list_3. Issubset (list_1) True

F, judging the parent set

>>> List_1.issuperset (list_3) True

G, symmetric difference set symmetri_difference (remove the intersection of list_1 and list_2 to save other remaining elements)

>>> list_1.symmetric_difference (list_2) {4, 5, 6, +, +}>>> list_1 ^ list_2 {33, 4, 5, 6, 88, 12}

   

Basic operation:

A, add (the collection operation has no insert action, can only be added)

>>>list_1{1, 3, 5, 6, 12, 77}>>> List_1.add (999)#Add a single>>> List_1.add ('Wang')>>>list_1{1, 3, 5, 6, 999, 12, 77,'Wang'}>>> List_1.update (['Alex','John','Tom'])#Add multiple items>>>list_1{1, 3, 5, 6, 999, 12, 77,'John','Tom','Wang','Alex'}

B, delete

>>>list_1{1, 3, 5, 6, 999, 12, 77,'John','Tom','Wang','Alex'}>>> List_1.remove (999)#Deleted by the Remove method, delete does not appear after deletion, delete need to explicitly specify the contents of the deleted item, because the collection is unordered, there is no index concept>>>list_1{1, 3, 5, 6, 12, 77,'John','Tom','Wang','Alex'}>>> List_1.remove (Wang)#quotation marks are required for stringsTraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'Wang'  is  notdefined>>> List_1.remove ('Wang')>>>list_1{1, 3, 5, 6, 12, 77,'John','Tom','Alex'}>>> List_1.pop ()#pop methods are randomly deleted, and deleted content is displayed after deletion1>>>List_1.pop ()3>>>List_1.pop ()5>>>List_1.pop ()6>>>List_1.pop ()12>>>List_1.pop ()77>>>List_1.pop ()'John'>>>list_1{'Tom','Alex'}

C, Discard Delete

>>>list_1{'Wang', 1, 3, 5, 6, 999, 12, 77,'John','Tom','Alex'}>>> List_1.discard (999)   >>>list_1{'Wang', 1, 3, 5, 6, 12, 77,'John','Tom','Alex'}>>> List_1.discard (888)#the difference between discard and remove is that discard does not error if it deletes content that does not exist, but remove will error>>> List_1.remove (999) Traceback (most recent): File"<stdin>", Line 1,inch<module>Keyerror:999

D, Length Len

>>> len (list_1)10>>> list_1{'Wang' John ' ' Tom ' ' Alex '}

E, member judgment (in and not in)

>>>list_1{'Wang', 1, 3, 5, 6, 12, 77,'John','Tom','Alex'}>>>'Wang' inchlist_1true>>> 11inchlist_1 False>>> 11 not inchlist_1true

F, copy (same as list, all belong to light copy)

Python Study notes third week

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.