【发布时间】:2016-08-17 23:44:22
【问题描述】:
我正在编写一个脚本来从多个圆柱体的外部圆柱体中减去内部圆柱体。
例如:x = pi*[n+1]**2 - pi*[n]**2
但是我不确定如何让 n 每次从例如 1 到 4 进行更改,我希望能够更改 n 并让代码在新值中运行而无需更改所有内容。
x = pi*[1]**2 - pi*[0]**2
x = pi*[2]**2 - pi*[1]**2
x = pi*[3]**2 - pi*[2]**2
x = pi*[4]**2 - pi*[3]**2
我试图让一个 while 循环工作,但我无法弄清楚如何引用 n 而不具体说明我想引用数组中的哪个数字。
任何帮助将不胜感激。
rs = 0.2 # Radius of first cylinder
rc = 0.4 # Radius of each cylinder (concrete)
rg = 1 # Radius of each cylinder (soil)
BW = 3 # No. cylinders (concrete)
BG = 2 # No. cylinders (soil)
v1 = np.linspace(rs, rc, num=BW) # Cylinders (concrete)
v2 = np.linspace(rc * 1.5, rg, num=BG) # Cylinders (soil)
n = np.concatenate((v1, v2)) # Combined cylinders
for i in range(BW + BG):
x = np.pi * (n[i + 1] ** 2) - np.pi * (n[i] ** 2)
【问题讨论】:
-
为什么不能具体说明要引用的数组编号?提供更多上下文代码会很有用。