Python built-in data structures list, set, dict, tuple (II)

Time：2021-10-18
#Difference between value transmission and address transmission
#For simple values, the value transfer operation is adopted, that is, the operation of parameters in the function does not affect the external variables
#For complex variables, address passing operation is adopted. At this time, the parameters in the function and external variables are the same content,
#Changes to this content anywhere affect the use of other variables or parameters

def a(n):
n = 300
print(n)
return None

def b(n):
n += 100
print(n)
return None

an = [1,2,3,4,5,6]
bn = 9

print(an)
a(an)
print(an)

print(bn)
b(bn)
print(bn)
[1, 2, 3, 4, 5, 6]
[1, 2, 300, 4, 5, 6]
[1, 2, 300, 4, 5, 6]
9
109
9

l = ['a', 'i love you', 45, 766, 5+4j]
l
['a', 'i love you', 45, 766, (5+4j)]

#Append inserts a content and appends it at the end
a = [i for i in range(1,5)]
print(a)
a.append(100)
print(a)
[1, 2, 3, 4]
[1, 2, 3, 4, 100]

#Insert: insert at the specified location
#Insert (index, date). The insertion position is in front of index
print(a)
a.insert(3, 666)
print(a)
[1, 2, 3, 4, 100]
[1, 2, 3, 666, 4, 100]

#Delete
#Del delete
#Pop, take out an element from the corresponding bit, that is, take out the last element
print(a)
last_ele = a.pop()
print(last_ele)
print(a)
[1, 2, 3, 666, 4, 100]
100
[1, 2, 3, 666, 4]

#Remove: deletes the element of the specified value from the list
#If the deleted value is not in the list, an error is reported
#That is, the operation of deleting the value specified in the list should use the try... Exception statement or judge first
# if x in list:
#    list.remove(x)
a.insert(4, 666)
print(a)
print(id(a))
a.remove(666)
print(a)
print(id(a))

#The output of the two ID values is the same, indicating that the remove operation is a direct operation on the original list
[1, 2, 3, 4, 666]
2261435601928
[1, 2, 3, 4]
2261435601928

#Clear: clear
print(a)
print(id(a))
a.clear()
print(a)
print(id(a))

#If you do not want the list address to remain unchanged, you can empty the list in the following ways
# a = list[]
# a = [ ]
[]
2261435601928
[]
2261435601928

#Reverse: flip in place

a = [1,2,3,4,5]
print(a)
print(id(a))

a.reverse()
print(a)
print(id(a))
[1, 2, 3, 4, 5]
2261441839496
[5, 4, 3, 2, 1]
2261441839496

#Exrend: expand the list, two lists, and splice one directly to the latter

a = [1,2,3,4,5]
b = [6,7,8,9,10]
print(a)
print(id(a))

a.extend(b)
print(a)
print(id(a))
[1, 2, 3, 4, 5]
2261441540872
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2261441540872

#Count: find the number of specified values or elements in the list
print(a)
a.append(8)
a.insert(4, 8)
print(a)
a_len = a.count(8)
print(a_len)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 8, 5, 6, 7, 8, 9, 10, 8]
3

#Copy: copy, shallow copy

#List type variable assignment example
a = [1,2,3,4,5,666]
print(a)
#List type, a simple assignment operation, is an address
b = a
b = 777
print(a)
print(id(a))
print(b)
print(id(b))

print("*" * 20)

#In order to solve the above problems, the list assignment needs to use the copy function
b = a.copy()
print(a)
print(id(a))
print(b)
print(id(b))

print("*" * 20)
b = 888
print(a)
print(b)
[1, 2, 3, 4, 5, 666]
[1, 2, 3, 777, 5, 666]
2195797555400
[1, 2, 3, 777, 5, 666]
2195797555400
********************
[1, 2, 3, 777, 5, 666]
2195797555400
[1, 2, 3, 777, 5, 666]
2195798283976
********************
[1, 2, 3, 777, 5, 666]
[1, 2, 3, 888, 5, 666]

#The difference between deep copy and shallow copy
#The reason for the following problem is that the copy function is a shallow copy function, that is, only one layer of content is copied
#Deep copy requires the use of specific tools
a = [1,2,3, [10 ,20 ,30]]
b = a.copy()
print(id(a))
print(id(b))
print(id(a))
print(id(b))
a = 666
print(a)
print(b)
2195798054792
2195796420296
2195798108872
2195798108872
[1, 2, 3, [10, 20, 666]]
[1, 2, 3, [10, 20, 666]]

Tuple tuple

• A tuple can be regarded as an immutable list

Tuple creation

#Create empty tuple
t = ()
print(type(t))

#Create a tuple with only one value
s = (1)
print(type(s))
print(s)

t = (1, )
print(type(t))
print(t)

t = 1,
print(type(t))
print(t)

#Create tuples of multiple values
t = (1,2,3,4,5)
print(type(t))
print(t)

t = 1,2,3,4,5
print(type(t))
print(t)

#Create with other structures
l = [1,2,3,4,5]
t = tuple(l)
print(t)
<class 'tuple'>
<class 'int'>
1
<class 'tuple'>
(1,)
<class 'tuple'>
(1,)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)

Characteristics of tuples

• It’s a sequence table, ordered
• Tuple data values can be accessed and cannot be modified
• Tuple data can be of any type
• In short, all features of list, except modifiable, tuples have
• It means that list as like as two peas, such as index, fragment, sequence addition, multiplication, membership operation, etc.
#Index operation
t = (1,2,3,4,5)
print(t)
5

#Exceedance error
print(t)
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-22-0db0bf4ec3b5> in <module>
1 # out of standard error
----> 2 print(t)

IndexError: tuple index out of range
t = (1,2,3,4,5,6)
t1 = t[1::2]
print(id(t))
print(id(t1))
print(t1)

#The slice can exceed the standard
t2 = t[2:100]
print(t2)
2195798058760
2195797607552
(2, 4, 6)
(3, 4, 5, 6)

t1 = (1,2,3)
t2 = (4,5,6,7)

print(t1)
print(id(t1))
t1 += t2
print(t1)
print(id(t1))

#The above operation is similar to
t1 = (1,2,3)
t1 = (2,3,4)

#The of tuple cannot be modified means that the content cannot be modified
#Modifying the tuple content will result in an error
t1 = 100
(1, 2, 3)
2195798298200
(1, 2, 3, 4, 5, 6, 7)
2195795953560

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-32-e65ebb898657> in <module>
16 # tuple cannot be modified, which means the content cannot be modified
17 # modifying tuple content will result in an error
---> 18 t1 = 100

TypeError: 'tuple' object does not support item assignment
#Tuple multiplication
t = (1,2,3)
t = t * 3
print(t)
(1, 2, 3, 1, 2, 3, 1, 2, 3)

#Member detection
t = (1,2,3)
if 2 in t:
print("Yes")
else:
print("No")
Yes

#Tuple traversal, generally for
#1. Single layer tuple traversal
t = (1,2,3,"ruochen", "i", "love")
for i in t:
print(i, end=" ")
1 2 3 ruochen i love

#2. Traversal of double tuples
t = ((1,2,3), (2,3,4), ("i", "love", "you"))

#The traversal of the above tuples can be as follows
# 1.

for i in t:
print(i)

for k,m,n in t:
print(k, "--", m, "--", n)
(1, 2, 3)
(2, 3, 4)
('i', 'love', 'you')
1 -- 2 -- 3
2 -- 3 -- 4
i -- love -- you

The VBA ide displays a project that is not open. The solution cannot be operated

Reading time |0.24 minutes word count |388 characters primary coverage |1. Introduction & background “VBA ide displays the project that is not open, and the solution cannot be operated” Author | SCscHero Writing time | 2021/12/29 PM7:28 Article type |Series Degree of completion |Completed motto Every great cause has a trivial beginning. 1、 Introduction & […]