import numpy as np
#综合练习题1.
# (1)
shop=np.array(["大润发","沃尔玛","好德","农工商"])
fruits=np.array(["苹果","梨","香蕉","橘子","芒果"])
#(2)
price=np.random.randint(4,10,size=(4,5))
print("初始价格数组:")
print(price)
#(3)
price[shop=='大润发',fruits=='苹果']+=1
price[shop=='好德',fruits=='香蕉']+=1
print("更新后的价格数组 (增加价格):")
print(price)
#(4)
price[shop=='农工商']-=2
print("更新后的价格数组 (减少价格):")
print(price)
#(5)
average=price[:,(fruits=='苹果')|(fruits=='芒果')].mean(axis=0)
print(f"苹果和芒果的销售均价分别是:{average}")
#(6)
orange_price=price[:,fruits=='橘子'].flatten()
max_orange=orange_price.max()
max_orange_shop=shop[orange_price==max_orange]
print(f"橘子价格最贵的超市是:{','.join(max_orange_shop)}")
import numpy as np
#综合练习题2.
#(1)
randwalk=np.random.normal(0,1,size=(3,10))
print(f"初始化数组:{randwalk}")
#(2)
position=randwalk.cumsum(axis=1)
print(f"每步走完的位置:{position}")
#(3)
dists=np.sqrt(position[0]**2+position[1]**2+position[2]**2)
np.set_printoptions(precision=2)
print(f"每步走完与原点的距离:{dists}")
#(4)
print(f"物体在z轴上到达的最远距离是{np.abs(position[2]).max():.2f}")
#(5)
print(f"物体距离原点最近的距离是:{dists.min():.2f}")
# 拓展: 随机游走的轨迹
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(position[0], position[1], position[2], marker='o')
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_zlabel('Z轴')
ax.set_title('随机游走轨迹')
plt.show()