[问题] list[:] & list 差别

楼主: DearYoyoDon (yoshito)   2016-03-27 11:17:39
def _product_of_two_vectors_sample_(a, b):
if len(a[0]) != len(b):
return None
# Create the result matrix and fill it with zeros
output_list=[]
temp_row=len(b[0])*[0]
for r in range(len(a)):
作者: LiloHuang (十年一刻)   2016-03-27 11:25:00
temp_row[:] 会做出一份拷贝,反之则是参照原先的list>>> foo = [1,2,3]>>> id(foo)33920784>>> bar = foo>>> id(bar)33920784>>> bar = foo[:]>>> id(bar)33919424留意到了 object id 改变了吗 :P
作者: ghostreporty (ghost)   2016-03-27 12:50:00
这有点像Java new的概念
作者: paulyanzi (消失)   2016-03-27 13:58:00
原来有 id 这个东西,id 这功能平常有什么机会使用到吗
作者: LiloHuang (十年一刻)   2016-03-27 15:01:00
当需要识别物件或者除错时,就是使用 id(obj) 的场合而 CPython 刚好使用物件的内存位址来当作 id延伸阅读官方文件 https://goo.gl/g3mzEZ
楼主: DearYoyoDon (yoshito)   2016-03-28 01:32:00
大概懂了!! 感谢~ >////<
作者: ENEP (Moo)   2016-03-28 01:54:00
推 原来[:]会作copy 那dict有类似的作法吗
作者: uranusjr (←這人是超級笨蛋)   2016-03-28 02:08:00
dict 直接呼叫 .copy() 即可

Links booklink

Contact Us: admin [ a t ] ucptt.com