pythonbook/实例学习Numpy与Matplotlib/Numpy分片.py

43 lines
965 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
# reshape
b1 = np.arange(15)
b2 = b1.reshape((3,5))
# b1 = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
# b2 = [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
print(f"b1 = {b1} \n ")
print(
f"b1[0:5] = {b1[0:5]} \n "
f"b1[-1] = {b1[-1]} \n "
f"b1[0:-1] = {b1[0:-1]} \n "
f"b1[:] = {b1[:]} \n "
f"b1[5:-1] = {b1[5:7]} \n "
f"b1[::] = {b1[::]} \n "
f"b1[::2] = {b1[::2]} \n "
f"b1[::-1] = {b1[::-1]} \n ")
print( f"b2 = {b2} \n ")
print(
f"b2[0:-1] = {b2[0:-1]} \n "
f"b2[0,-1] = {b2[0,-1]} \n "
# 0可以不写。以下两个柿子结果不一样在numpy中使用","做多维索引
f"b2[0:2,0:3] = {b2[:2,:3]} \n "
f"b2[:2][:3] = {b2[:2][:3]} \n "
# 取第0行
f"b2[0,:] = {b2[0,:]} \n "
# 取第0列
f"b2[:,0] = {b2[:,0]} \n "
# 取最后1列
f"b2[:,-1] = {b2[:,-1]} \n "
# 行全取,列全取,行倒过来
f"b2[::-1,::] = {b2[::-1,::]} \n "
)