我有以下代码:
for i in range(N):
# Dispersed Source
theta = np.random.uniform(0, np.pi, 1)
phi = np.random.uniform(0, 2 * np.pi, 1)
R = np.random.uniform(0, Ro, 1)
x = R * np.sin(theta) * np.cos(phi)
y = R * np.sin(theta) * np.sin(phi) # ?
z = R * np.cos(theta)
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
particle_trace = go.Scatter3d(
x=x_vec,
y=y_vec,
z=z_vec,
mode='lines'
)
data.append(particle_trace)
for j in range(int(nu)):
x = x_vec[-1]
y = y_vec[-1]
z = z_vec[-1]
x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec_1,
y=y_vec_1,
z=z_vec_1,
mode='lines'
)
data.append(particle_trace_fiss)
问题是for循环需要继续这样:
for j in range(int(nu)):
x = x_vec[-1]
y = y_vec[-1]
z = z_vec[-1]
x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec_1,
y=y_vec_1,
z=z_vec_1,
mode='lines'
)
data.append(particle_trace_fiss)
for k in range(int(nu1)):
x = x_vec_1[-1]
y = y_vec_1[-1]
z = z_vec_1[-1]
x_vec_2, y_vec_2, z_vec_2, nu2 = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec_2,
y=y_vec_2,
z=z_vec_2,
mode='lines'
)
data.append(particle_trace_fiss)
for l in range(int(nu2)):
x = x_vec_2[-1]
y = y_vec_2[-1]
z = z_vec_2[-1]
x_vec_3, y_vec_3, z_vec_3, nu3 = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec_3,
y=y_vec_3,
z=z_vec_3,
mode='lines'
)
data.append(particle_trace_fiss)
...
因为nu的值是一个随机数或零(从particle_func函数给出),我需要的循环数可能会永远持续下去。也就是说,我可以复制/粘贴
for k in range(int(nu#)):...
data.append(particle_trace_fiss)
无数次并获得我想要的东西(其中nu#表示前一循环提供的nu的值。
道歉,如果我在问题中遗漏了清晰度,我将很乐意回复任何要求澄清的评论。所有帮助都提前感谢,谢谢!!
答案 0 :(得分:1)
您似乎只需要将for
包裹在while
循环中,也可以使用任意截止值。
此代码:
for i in range(N):
# Dispersed Source
theta = np.random.uniform(0, np.pi, 1)
phi = np.random.uniform(0, 2 * np.pi, 1)
R = np.random.uniform(0, Ro, 1)
x = R * np.sin(theta) * np.cos(phi)
y = R * np.sin(theta) * np.sin(phi) # ?
z = R * np.cos(theta)
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
particle_trace = go.Scatter3d(
x=x_vec,
y=y_vec,
z=z_vec,
mode='lines'
)
data.append(particle_trace)
for j in range(int(nu)):
x = x_vec[-1]
y = y_vec[-1]
z = z_vec[-1]
x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec_1,
y=y_vec_1,
z=z_vec_1,
mode='lines'
)
data.append(particle_trace_fiss)
成为这段代码:
for i in range(N):
# Dispersed Source
theta = np.random.uniform(0, np.pi, 1)
phi = np.random.uniform(0, 2 * np.pi, 1)
R = np.random.uniform(0, Ro, 1)
x = R * np.sin(theta) * np.cos(phi)
y = R * np.sin(theta) * np.sin(phi) # ?
z = R * np.cos(theta)
arbitrary_limit = 10
x_vec = [x]
y_vec = [y]
z_vec = [z]
nu_vec = [1]
nu = nu_vec[-1] # Updated - keep a list of values of nu
while int(nu) != 0:
for j in range(int(nu)):
x = x_vec[-1]
y = y_vec[-1]
z = z_vec[-1]
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
nu_vec.append(nu)
particle_trace = go.Scatter3d(
x=x_vec,
y=y_vec,
z=z_vec,
mode='lines'
)
data.append(particle_trace)
我想问题是,您将如何决定何时停止?您可以采取哪些措施来限制nu
的价值吗?
更新:根据您的评论,我删除了arbitrary_limit
代码。我还添加了nu_vec
来跟踪nu的所有值,根据您在另一个尝试的答案中的解释。
答案 1 :(得分:0)
我以为我可以使用以下内容:
for i in range(N):
# Central Point Source
# z = 0
# x = 0
# y = 0
# Dispersed Source
theta = np.random.uniform(0, np.pi, 1)
phi = np.random.uniform(0, 2 * np.pi, 1)
R = np.random.uniform(0, Ro, 1)
x = R * np.sin(theta) * np.cos(phi)
y = R * np.sin(theta) * np.sin(phi) # ?
z = R * np.cos(theta)
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
particle_trace = go.Scatter3d(
x=x_vec,
y=y_vec,
z=z_vec,
mode='lines'
)
data.append(particle_trace)
while nu != 0:
x = x_vec[-1]
y = y_vec[-1]
z = z_vec[-1]
for j in range(int(nu)):
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
particle_trace_fiss = go.Scatter3d(
x=x_vec,
y=y_vec,
z=z_vec,
mode='lines'
)
data.append(particle_trace_fiss)
因为它表现得像我想的那样。但是,我很困惑:
for j in range(int(nu)):
x_vec, y_vec, z_vec, nu = particle_func(x, y, z)
因为nu的新值将与for循环范围内的nu值一样多。每个nu值都很重要。也就是说,我打算生成一定数量的nu值。上述问题是每个nu值被for循环中的下一个nu值覆盖。如果生成5个nu值,我在技术上需要独立地对每个nu值运行另一个for循环,并且循环继续。我希望这有道理,有什么想法吗?我想我可以将所有的nu值附加到一个向量中并得到它们吗?