Basic data type supplementary summary, python copy set of shades

First, the basic data types are summarized supplement

 1. Check all the ways and means of str

 

2. List: delete elements in a loop, error-prone or error (rosy)

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]

Variable data type, delete the data changes. Error: list assignment index out of range

#第一种
# 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)

When the circulation list of dictionaries, do not delete things inside.

Second, the collection

Sets are unordered, unique data set that is inside the hash element (immutable type), but the hash itself is not set (it is set dictionary do not bond). The following is a collection of the most important points:

  De-emphasis , the list becomes a collection, automatically go heavier.

  Relationship test, before the intersection of two sets of test data, poor, union and other relations.

1, to create the collection.

set1 = set({1,2,'barry'})
set2 = {1,2,'barry'}
print(set1,set2)  # {1, 2, 'barry'} {1, 2, 'barry'}

2, increasing the collection. add update

Copy the code
= {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)
Copy the code

3, delete the collection.

= {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

Copy the code
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.
Copy the code

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.

Copy the code
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']]
Copy the code

  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.

Copy the code
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
Copy the code

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.

Copy the code
# 同一代码块下
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
Copy the code

 

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]]

Guess you like

Origin www.cnblogs.com/RevelationTruth/p/11488728.html