array([[ 0, 1, 2],
[ 6, 7, 8],
[12, 13, 14]])
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
此时r=
array([[ 0, 0, 0, 3, 4, 5],
[ 0, 0, 0, 9, 10, 11],
[ 0, 0, 0, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 30, 30, 30, 30, 30]])
1 r_copy = r.copy()
2 r_copy
array([[ 0, 0, 0, 3, 4, 5],
[ 0, 0, 0, 9, 10, 11],
[ 0, 0, 0, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 30, 30, 30, 30, 30]])
1 r_copy[:] = 10
2 print(r_copy, '
')
3 print(r)
[[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]]
[[ 0 0 0 3 4 5]
[ 0 0 0 9 10 11]
[ 0 0 0 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 30 30 30 30 30]]
1 test = np.random.randint(0, 10, (4,3))
2 test
array([[0, 7, 9],
[9, 2, 3],
[7, 5, 9],
[4, 8, 4]])
1 for row in test:
2 print(row)
[0 7 9]
[9 2 3]
[7 5 9]
[4 8 4]
1 for i in range(len(test)):
2 print(test[i])
[0 7 9]
[9 2 3]
[7 5 9]
[4 8 4]
1 for i, row in enumerate(test):
2 print('row', i, 'is', row)
row 0 is [0 7 9]
row 1 is [9 2 3]
row 2 is [7 5 9]
row 3 is [4 8 4]
1 test2 = test**2
2 test2
array([[ 0, 49, 81],
[81, 4, 9],
[49, 25, 81],
[16, 64, 16]])
1 for i, j in zip(test, test2):
2 print(i,'+',j,'=',i+j)
[0 7 9] + [ 0 49 81] = [ 0 56 90]
[9 2 3] + [81 4 9] = [90 6 12]
[7 5 9] + [49 25 81] = [56 30 90]
[4 8 4] + [16 64 16] = [20 72 20]