Python list 遇到的问题

1.list“+” 运算

<list += > diff. <ndarray +=>

list1 += list2是追加,而不是加法运算

list1 = [0,0,0]
list2 = [1,1,1]
list1 += list2
list1
[0, 0, 0, 1, 1, 1]

ndarray1 += ndarray2是加法运算,要求维度相同

nda1 = np.arange(3)
nda2 = np.arange(3)
nda1 += nda2
nda1
array([0, 2, 4])

2.关于list的引用(具体来说是元素为引用的list;ndarray也是如此)

Matrix1 = [[0,0,0],[1,1,1]]
list1 = Matrix1[0]
list2 = Matrix1[1]
list1 += list2
Matrix1
[[0, 0, 0, 1, 1, 1], [1, 1, 1]]

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

python居然这么多基础操作都是给引用而不是深拷贝 - -

Numpy的视图与拷贝

赋值操作(=)与切片都是浅拷贝…

猜你喜欢

转载自www.cnblogs.com/peanutk/p/9940438.html
今日推荐