numpy学习笔记(三):索引和切片

import numpy as np
from numpy import pi

a = np.arange(10)**3

b = a[2:5]
print("b=", b)

print("a[2]=", a[2])

a[:6:2] = 1000  # from start to position 6, exclusive, set every 2nd element to 1000
print(a)

c = a[::-1]   # reverse  a doesn't change
print(c)
for i in a:
    print("%.1f" % i ** (1 / 3.))


def f(x, y):
    return x * 10 + y


d = np.fromfunction(f, (4, 6), dtype=np.int64)
print(d)

print(d[-1])   # d[-1, :]
print(d[2:4, :])
print(d[3, 4])

e = np.array([[[1, 2, 3], [4, 5, 6]],
              [[7, 8, 9], [10, 11, 12]]])
print(e.shape)

print(e[0, :, :])
print(e[:, :, 1])

for row in d:
    print(row)

for block in e:
    print(block)

for row in e[1, :, :]:
    print(row)

for item in e.flat:
    print(item)

猜你喜欢

转载自blog.csdn.net/qq_30986521/article/details/80819151
今日推荐