Python Learning 7

Source: Internet
Author: User

1. In the cycle of lists and dictionaries, you cannot delete them directly

Remove 1th, 3-bit elements of lis = [11,22,33,44,55]
# Method 1 " " lis = [11,22,33,44,55]li = []for i in Range (len (LIS)):    If I% 2 = = 0:        li.append (lis[i]) lis = Liprint (LI) c6> '# Method 2lis = [11,22,33,44,55= tuple (LIS) for in range (len (LIS)):     if i% 2 = = 1:        lis.remove (Tu[i])print(LIS)

Delete dictionary dic = {' K1 ': ' v1 ', ' K2 ': ' V2 ', ' A3 ': ' V3 '} in corresponding key-value pairs

#Dic1 = {}DIC = {'K1':'v1','K2':'v2','A3':'v3'}#For i in DIC:#if ' K ' isn't in I:#Dic1.setdefault (I,dic[i])#dic = Dic1#print (DIC)#L = []#For i in DIC:#if ' K ' in I:#l.append (i)#For i in L:#del Dic[i]#print (DIC)

Meta-group

TU1 = (1)  int
TU2 = (1,) tuple

2. Collection
The set is mutable, the elements inside it are immutable, and the collection is unordered and non-repeating.
Collection has additions, deletions, queries, orthogonal, etc.
1. Increase
. Add () / . Update ()
#Set1 = set ({-)#Set2 = {1,2,3,[3,4],{' name ': ' Alwx '}} #错的#print (Set1,set2)Set1 = {'Alex','Wusir','Ritian','Egon','Barry','Barrry'}#add adds an elementSet1.add ('Schoolgirl')Print(Set1)#Update Iteration AddSet1.update ('ABC')Print(Set1)

2. Delete

. Pop ()/. Remove ()/Del/.clear ()

##删除 Pop#print (Set1.pop ()) #随机删除 with return value#print (Set1)###按元素删除 Remove#set1.remove (' Alex ')#print (Set1)#emptying the collection#set1.clear ()#print (Set1)#del Delete entire collection#del Set1#print (Set1)

3. No change, check can only follow for the loop, because the collection is unordered

 for inch Set1:     Print (i)

4. Intersection, set, inverse intersection, difference set, subset, superset

&,. Intersection ()

| ,. Union ()

^,. Symmetric_difference ()

Set1-set2,. Difference ()

Set1 < Set2 returns the bool value, which is the inclusion relationship of the comparison collection

#the special place can seek orthogonal difference set#intersectionSet1 = {1,2,3,4,5}set2= {4,5,6,7,8}Print(Set1 & Set2)#{4,5}Print(Set1.intersection (Set2))#{4,5}Set3 = Set1 &Set2#and setPrint(Set1 |Set2)Print(Set2.union (Set1)).#Inverse intersection In addition to the intersection of the setPrint(Set1 ^Set2)Print(Set1.symmetric_difference (Set2))#Difference SetPrint(Set1-set2)#Set1 's uniquePrint(Set1.difference (Set2))#subset and superset comparisons contain relationships, return bool valuesSet1 = {}set2= {1,2,3,4,5}Print(Set1 <Set2)Print(Set1.issubset (Set2))
View Code

5. Examples remove duplicate elements from a list

Li = [1,2,33,33,2,1,4,5,6,6= Set (LI)print= list (set1)print( Li

Collections can be frozen and become immutable data types

# The set itself is mutable, the inner element is immutable, and the set freezes s = frozenset ('Barry')   #  becomes immutable data type, read-only, unordered print(S,type (s)) for in s:      Print(i)

6. Depth Copy

After the assignment operation, two variables point to the same memory address, changing one will cause another change

After the copy operation, two variable memory addresses are not the same, changing one will not cause another change

L1 = [A]l2=L1l1.append ('a')Print(L1,L2)#CopyL1 = [A]l2=l1.copy ()Print(L1,L2)Print(ID (L1), id (L2)) L2.append ('a')Print(L1,L2) [1, 2, 3,'a'] [1, 2, 3,'a'][1, 2, 3] [1, 2, 3]1921628470728 1921628348040[1, 2, 3] [1, 2, 3,'a']

The memory address of the list is unchanged after the copy operation

L1 = [1,2,[4,5,6],3]l2=l1.copy ()Print(L1,id (L1))Print(L2,id (L2)) L1.append ('a')Print(L1,L2) l1[2].append ('a')Print(L1,L2)Print(ID (l1[2]))Print(ID (l2[2]))###################[1, 2, [4, 5, 6], 3] 1921627608072[1, 2, [4, 5, 6], 3] 1921628470728[1, 2, [4, 5, 6], 3,'a'] [1, 2, [4, 5, 6], 3][1, 2, [4, 5, 6,'a'], 3,'a'] [1, 2, [4, 5, 6,'a'], 3]19216284708561921628470856

memory address of depth copy list changed, two list does not affect each other

But L2 = l1[:] The assignment operation causes the memory address of the two lists to be different, but the values will affect each other and change

ImportCOPYL1= [1,2,[4,5,6],3]l2=copy.deepcopy (L1)Print(L1,id (L1))Print(L2,id (L2)) l1[2].append ('a')Print(L1,L2) L1= [1,[1],2,3,4]l2=l1[:] **********************l1[1].append ('a')Print(L1,id (L1))Print(L2,id (L2))Print(L1[1] isL2[1])#########################[1, 2, [4, 5, 6], 3] 1921630006344[1, 2, [4, 5, 6], 3] 1921627608072[1, 2, [4, 5, 6,'a'], 3] [1, 2, [4, 5, 6], 3][1, [1,'a'], 2, 3, 4] 1921628470856[1, [1,'a'], 2, 3, 4] 1921630006344True

The loop output list element and its ordinal

Li = ['Alex','taibai','wusir', ' Egon ' ] for in Li:    Print(Li.index (i) + 1, i)   for in enumerate (li,1):    print(index,i)

Python Learning 7

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.