Skip to content

Instantly share code, notes, and snippets.

@zhuqling
Last active December 15, 2015 06:39
Show Gist options
  • Save zhuqling/5218135 to your computer and use it in GitHub Desktop.
Save zhuqling/5218135 to your computer and use it in GitHub Desktop.
结论1:字符为不可变类型,因此不存在浅复制问题,并且不能更改字符串内容 结论2,用不可变类型复制出来的列表(可变类型),不可变类型变更,不会影响列表,因为不可变类型其实是引用到了新地址空间 结论3,只有用可变类型复制的内容,当原可变类型变化时,复制出来的变量内容即更新 结论4,使用列表内涵生成的列表,等同于使用for循环添加列表元素,不受浅复制影响
"""
>>> char = 'a'
>>> str = char * 3
>>> str
'aaa'
>>> str[0] = 'b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
# 结论1:字符为不可变类型,因此不存在浅复制问题,并且不能更改字符串内容
>>> str[0]
'a'
>>> list = [char] * 3
>>> list
['a', 'a', 'a']
>>> list[0] = 'b'
>>> list
['b', 'a', 'a']
>>> char = 'c'
>>> list
['b', 'a', 'a']
# 结论2,用不可变类型复制出来的列表(可变类型),不可变类型变更,不会影响列表,因为不可变类型其实是引用到了新地址空间
>>> list2 = [[char] * 3] * 3
>>> list2
[['c', 'c', 'c'], ['c', 'c', 'c'], ['c', 'c', 'c']]
>>> char = 'b'
>>> list2
[['c', 'c', 'c'], ['c', 'c', 'c'], ['c', 'c', 'c']]
>>> list2[0][0] = 'h'
>>> list2
[['h', 'c', 'c'], ['h', 'c', 'c'], ['h', 'c', 'c']]
>>> list2[1][1] = 'n'
>>> list2
[['h', 'n', 'c'], ['h', 'n', 'c'], ['h', 'n', 'c']]
# 结论3,只有用可变类型复制的内容,当原可变类型变化时,复制出来的变量内容即更新
>>> list3 = [[char for x in range(10)] for y in range(10)]
>>> list3
[['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']]
>>> char = 'h'
>>> list3
[['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']]
>>> list3[0][0] = 'h'
>>> list3
[['h', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']]
# 结论4,使用列表内涵生成的列表,等同于使用for循环添加列表元素,不受浅复制影响
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment