python基础之数据内存相关
Posted sundy08
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础之数据内存相关相关的知识,希望对你有一定的参考价值。
数据内存相关
示例-:
v1 = [11,22,33]
v2 = [11,22,33]
v1 = 666
v2 = 666
v1 = "asdf"
v2 = "asdf"
v1 = [11,22,33]
v2 = [11,22,33]
v1 = 666
v2 = 666
v1 = "asdf"
v2 = "asdf"
# 按理 v1 和 v2 应该是不同的内存地址。特殊:
1. 整型: -5 ~ 256 (id一样)
2. 字符串:(常见字符串)"alex",‘asfasd asdf asdf d_asdf ‘ (包含特殊字符)"f_*" * 3(或者相乘大于3) 还有列表 - 重新开辟内存。
1. 整型: -5 ~ 256 (id一样)
2. 字符串:(常见字符串)"alex",‘asfasd asdf asdf d_asdf ‘ (包含特殊字符)"f_*" * 3(或者相乘大于3) 还有列表 - 重新开辟内存。
示例二:
v1 = [11,22,33,44] (v1重新赋值后,旧的值将被解析器当成垃圾清理掉)
v1 = [11,22,33]
v1 = [11,22,33]
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, 33]
[11, 22, 33]
示例三:
v1 = [11,22,33]
v2 = v1
v2 = v1
# 练习1 (内部修改)
v1 = [11,22,33]
v2 = v1
v1.append(666)
print(v2) # 含 666
v1 = [11,22,33]
v2 = v1
v1.append(666)
print(v2) # 含 666
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, 33, 666]
[11, 22, 33, 666]
# 练习2:(赋值)
v1 = [11,22,33]
v2 = v1
v1 = [1,2,3,4]
print(v2)
v1 = [11,22,33]
v2 = v1
v1 = [1,2,3,4]
print(v2)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, 33]
[11, 22, 33]
# 练习3:(重新赋值)
#重新赋值
v1 = ‘sundy‘
v2 = v1
v1 = ‘amy‘
print(v2)
#重新赋值
v1 = ‘sundy‘
v2 = v1
v1 = ‘amy‘
print(v2)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
sundy
#示例四:
v = [1,2,3]
values = [11,22,v]
练习1:
v.append(9)
print(values)
values = [11,22,v]
练习1:
v.append(9)
print(values)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, [1, 2, 3, 9]]
练习2
v = [1,2,3]
values = [11,22,v]
values[2].append(66)
print(values)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, [1, 2, 3, 66]]
[11, 22, [1, 2, 3, 66]]
练习3:
v = [1,2,3]
values = [11,22,v]
v = 999
print(values)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, [1, 2, 3]]
v = [1,2,3]
values = [11,22,v]
v = 999
print(values)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[11, 22, [1, 2, 3]]
练习4:
v = [1,2,3]
values = [11,22,v]
v = 999
values[2] = 666
print(v)
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[1, 2, 3]
查看内存地址
v1 = [1,2,3]
v2 = v1
v1.append(999)
print(v1,v2)
print(id(v1),id(v2))
v2 = v1
v1.append(999)
print(v1,v2)
print(id(v1),id(v2))
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[1, 2, 3, 999] [1, 2, 3, 999]
1188414396872 1188414396872
[1, 2, 3, 999] [1, 2, 3, 999]
1188414396872 1188414396872
v1 = [1,2,3]
v2 = v1
print(id(v1),id(v2))
v1 = 999
print(id(v1),id(v2))
v2 = v1
print(id(v1),id(v2))
v1 = 999
print(id(v1),id(v2))
D:python3.6python3.6.exe D:/python_code/day01/day06.py
2339259718088 2339259718088
2339258862448 2339259718088
问题: == 和 is有什么区别?
== 用于比较值是否相等。
is 用于比较内存地址是否相等
a = [[1,2,3],[4,5,6]]
b = []
for i in a:
print(i)
b.append(i)
print(id(a),id(b))
print(id(a[1]),id(b[1]))
D:python3.6python3.6.exe D:/python_code/day01/day06.py
[1, 2, 3]
[4, 5, 6]
2591750527496 2591749628168
2591750527304 2591750527304
[1, 2, 3]
[4, 5, 6]
2591750527496 2591749628168
2591750527304 2591750527304
以上是关于python基础之数据内存相关的主要内容,如果未能解决你的问题,请参考以下文章