lis = [11,22,33,44,55]
# for i in range(len(lis)):
# print(i) # i = 0 i = 1 i = 2
# del lis[i]
# print(lis) # [11,22,33,44,55] [22, 44, 55] [22, 44]
#第一种
# lis = lis[::2]
# print(lis)
#第二种
# l1 = []
# for i in lis:
# if lis.index(i) % 2 == 0:
# l1.append(i)
# lis = l1
# print(lis)
# lis = [11,22,33,44,55]
# # for i in range(len(lis)-1,-1,-1):
# # if i % 2 == 1:
# # print(i)
# # del lis[i]
# # print(lis)
# # print(lis)
# dic = dict.fromkeys([1,2,3],'春哥')
# print(dic)
Print (DIC)
# L = []
# for I in DIC:
# IF 'K' in I:
# l.append (I)
# for I in L:
# del DIC [I]
# Print (DIC)
# converted to bool value
# 0 '' [] () {} SET ()
# ancestral If there is only one neuron progenitor element does not increase, and that is what this element type, that is what type.
# TU1 = (. 1)
# TU2 = (. 1,)
# Print (TU1, type (TU1))
# Print (TU2, type (TU2))
# TU1 = ([. 1])
# TU2 = ([. 1],)
# Print (TU1, of the type (TU1))
# Print (TU2, of the type (TU2))
# dic = dict.fromkeys([1,2,3,],3)
# dic[1] = 4
# print(dic)
set1 = set({1,2,'barry'})
set2 = {1,2,'barry'}
print(set1,set2) # {1, 2, 'barry'} {1, 2, 'barry'}
= {setl 'Alex', 'wusir', 'ritian', 'Egon', 'Barry'}
set1.add ( 'View Goddess')
Print (setl)
#update: Iterative increased similar list Extend
set1.update ( 'A')
Print (setl)
set1.update ( 'teacher')
Print (setl)
set1.update ([l, 2,3])
Print (setl)
= {setl ' Alex ' , ' wusir ' , ' ritian ' , ' Egon ' , ' Barry ' }
set1.remove ( ' Alex ' ) # removes an element
print (setl)
set1.pop () # random element to delete a
print (SET1)
set1.clear () # empty collection
Print (SET1)
del SET1 # delete a collection
Print (SET1)
Query: check cycle
4. Other sets of operations:
4.1 intersection. (Or & intersection)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2) # {4, 5}
print(set1.intersection(set2)) # {4, 5}
4.2 union. (| Or union)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7,8}
print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7,8}
4.3 difference sets. (- or difference)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2) # {1, 2, 3}
print(set1.difference(set2)) # {1, 2, 3}
4.4 Anti intersection. (^ Or symmetric_difference)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}
4.5 subset superset
l, 2,3} = {set1
SET2 = {1,2,3,4,5,6}
Print (set1 <SET2)
Print (set1.issubset (SET2)) # two identical, are described set1 is set2 subset.
Print (set2> set1)
Print (set2.issuperset (set1)) # two identical, are explained set1 set2 is a superset.
5, frozenset immutable collections, so the set becomes immutable types.
s = frozenset('barry')
print(s,type(s)) # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>
Copy depth
To ask you, what is the copy? Copy is a transliteration of the word, in fact, he is the copy from the English transliteration of the word, what is the copy? copy is actually a copy, also known as a copy. In fact, the depth of copy is complete copy, and a copy of part of the meaning.
1, look at the assignment operator.
l1 = [1,2,3,['barry','alex']]
l2 = l1
l1[0] = 111
print(l1) # [111, 2, 3, ['barry', 'alex']]
print(l2) # [111, 2, 3, ['barry', 'alex']]
l1[3][0] = 'wusir'
print(l1) # [111, 2, 3, ['wusir', 'alex']]
print(l2) # [111, 2, 3, ['wusir', 'alex']]
For the assignment operator is, l1 and l2 points to the same memory address, so they are exactly the same, in for example, such as Zhangsanlisi shared together, then for the living room, they are common, Joe Smith can be used John Doe can also be used, but suddenly one day Joe Smith replaced the TV in the living room of projection, then use the living room when John Doe, did not want to watch TV, but rather a projection, right? l1, l2 point that any change in a variable list with a list of the variables after using the rest of the list, this list is the list after the change.
2, shallow copy copy.
The same block of code #:
L1 = [. 1, 'Bai', True, (l, 2,3), [22 is, 33 is]]
L2 = l1.copy ()
Print (ID (L1), ID (L2)) 2713214468360 2713214524680 #
Print (ID (L1 [-2]), ID (L2 [-2])) # 2,547,618,888,008 2,547,618,888,008
Print (ID (L1 [-1]), ID (L2 [-1])) # 2547620322952 2547620322952
# different code blocks:
>>> L1 = [. 1, 'Bai', True, (. 1, 2,. 3), [22 is, 33 is]]
>>> l1.copy L2 = ()
>>> Print (ID ( L1), ID (L2))
1,477,183,162,120 1,477,183,162,696
>>> Print (ID (L1 [-2]), ID (L2 [-2]))
1477181814032 1477181814032
>>> Print (ID (L1 [-1]), ID (L2 [-1]))
1,477,183,162,504 1,477,183,162,504
For shallow copy, it just re-created in memory opens up a space for a new list, but the elements in the new list with elements of the original list is common.
3, deep copy deepcopy.
# 同一代码块下
import copy
l1 = [1, 'alex', True, (1,2,3), [22, 33]]
l2 = copy.deepcopy(l1)
print(id(l1), id(l2)) # 2788324482440 2788324483016
print(id(l1[0]),id(l2[0])) # 1470562768 1470562768
print(id(l1[-1]),id(l2[-1])) # 2788324482632 2788324482696
print(id(l1[-2]),id(l2[-2])) # 2788323047752 2788323047752
# 不同代码块下
>>> import copy
>>> l1 = [1, '太白', True, (1, 2, 3), [22, 33]]
>>> l2 = copy.deepcopy(l1)
>>> print(id(l1), id(l2))
1477183162824 1477183162632
>>> print(id(0), id(0))
1470562736 1470562736
>>> print(id(-2), id(-2))
1470562672 1470562672
>>> print(id(l1[-1]), id(l2[-1]))
1477183162120 1477183162312
For deep copy, the list is re-created in memory, the data type of the variable list is re-created, immutable data types in the list are shared.
l1 = [1, 2, 3, 4, ['alex']]
l2 = l1[::]
l1[-1].append(666)
print(l2) # [1, 2, 3, 4, ['alex', 666]]