Python03.3. Copy depth

5. Copy shades

    Copy is actually a copy, also known as a copy;

    In fact, the depth of copy is complete copy, a copy of part of the meaning;

Shallow copy copy

Under the same block of code:

Import copy

l1 = [1, "jason", True, (1,2,3), [22, 33]]

l2 = l1.copy()

>>>print(id(l1), id(l2))

# 2713214468360 2713214524680

>>>print(id(l1[-2]), id(l2[-2]))

# 2547618888008 2547618888008

>>>print(id(l1[-1]),id(l2[-1]))

# 2547620322952 2547620322952

==================================================================

# Different code blocks:

l1 = [1, "jason";, True, (1, 2, 3), [22, 33]]

l2 = l1.copy()

>>>print(id(l1), id(l2))

1477183162696

>>>print(id(l1[-2]), id(l2[-2]))

1477181814032

>>>print(id(l1[-1]), id(l2[-1]))

1477183162504

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 public

Only one copy of a shallow copy data

Deep copy deepcopy

同一代码块下

import copy

l1 = [1, "jason", 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, "jason&", True, (1, 2, 3), [22, 33]]

l2 = copy.deepcopy(l1)

>>>print(id(l1), id(l2))

1477183162632

>>>print(id(0), id(0))

1470562736

>>>print(id(-2), id(-2))

1470562672

>>>print(id(l1[-1]), id(l2[-1]))

1477183162312

    For deep copy, the list is re-created in memory, the data type of the variable list is re-created, the data type list of immutable is common

6. hexadecimal conversion

python binary conversion

You can turn decimal hex hex hex Binary string results are

>>> hex(0b10)

'0x2'

>>> hex(10)

'0xa'

==================================================================

bin 可以十进制转2进制 16进制转2进制 结果都是字符串

>>> bin(10)

'0b1010'

>>> bin(0x2)

'0b10'

int 可以16进制转换十进制 2进制转换十进制

>>> int(0xe)

>>> int(0b100)

7.三元运算

python的三元运算:

变量名 = 变量1 if 条件判断成立 else 变量2

==================================================================

解释:条件成立 变量名值为变量1 否则为变量2

==================================================================

a=1

b=2

if a<b:

#一般条件语句的写法

k=a

else:

k=b

==================================================================

c=a if a<b else b

#三目运算符的写法

Guess you like

Origin www.cnblogs.com/cable-run/p/12161490.html