首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python中将值从最内部的while循环传递到外部for循环?

如何在python中将值从最内部的while循环传递到外部for循环?
EN

Stack Overflow用户
提问于 2017-02-26 01:41:24
回答 1查看 44关注 0票数 0

假设有一个已知中心(Xo,Yo,Zo)和半径Ro的大球体。它包含数以百万计的粒子,每个粒子具有已知的质量和相对于同一参照系的三维位置。在这个大球体中,有十几个较小的假想球体随机分布在3d空间中,但其他已知的位置。我想计算每个较小的球体内的粒子数目,因此,通过计算每个小球体(容器)内的粒子,计算出每个球(容器)的质量。

这是我的MWE片段,我在单个粒子上循环,看看它们是否在每个较小的球体中,然后对每个球体分别进行计数,从而得出每个较小球体的总质量:

代码语言:javascript
运行
复制
import numpy as np
from scipy.spatial.distance import euclidean 

### there is 10 small spheres inside Big Sphere each containing different number of particles and hence different masses
### small_sphere_id_array is the array of the IDs of the small spheres
### small_sphere_position_array is the array of the position of the center of small spheres
### small_sphere_radius_array is the array of the radii of small spheres
### small_sphere_mass_array is the array of the masses of small spheres
### particle_position_array is the array of the positions of particles inside the Big Sphere
### particle_mass_array is the array of the masses of particles inside the Big Sphere

for small_sphere_index in np.arange(0, 10)):

    for particle_index in np.arange(0, 6000000)):

        small_sphere_mass_array = []    
        small_sphere_mass = 0

        distances = euclidean(particle_position_array[particle_index], small_sphere_position_array[small_sphere_index])
        success_condition = (distances <= small_shpere_radius_array[small_sphere_index])

        while success_condition:
            small_sphere_mass += particle_mass_array[particle_index]         
            small_sphere_mass_array.append(small_sphere_mass)
            small_sphere_mass = np.sum(small_sphere_mass_array)

        else:
            break


    print('{}, {}'.format(small_sphere_id_array[small_sphere_index], small_sphere_mass))

我希望已经打印出10行(对应于10个小球体),他们的身份证,首先是他们的总质量。但是,这是我的输出:

代码语言:javascript
运行
复制
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0

特别是,我不能利用while循环的结果,把它移动到外for环,从而对所有粒子进行循环,得到每个小球体的单个非零总质量。我似乎正确地循环了10个小球体,但是由于质量最终为零,这意味着中间的for循环不能解释所有的6000000个粒子。(提示:所有粒子都有质量。)

EN

回答 1

Stack Overflow用户

发布于 2017-04-23 22:04:29

我刚刚(通过我的一个朋友)了解到,我把python和C++之间的语法混合在一起,因为与C++不同的是,在C++中,变量的赋值不能从内循环取到外部循环,python实际上能够通过循环之间的缩进来记住指定的值。在python中(与C++不同),一旦执行了一个循环并到达了它的末尾,然后通过一个缩进,python仍然可以回忆它正在计算的内容,因此可以访问该循环的最终产品。因此,我应该使用while循环,而不是使用if循环,以确保所有的粒子都已经根据条件进行了检查。另外,变量small_sphere_mass和small_sphere_mass_array的初始化应该在内部循环之外进行,这样我就不会强迫循环始终保持第一个零值。

代码语言:javascript
运行
复制
import numpy as np
from scipy.spatial.distance import euclidean 

### there is 10 small spheres inside Big Sphere each containing different number of particles and hence different masses
### small_sphere_id_array is the array of the IDs of the small spheres
### small_sphere_position_array is the array of the position of the center of small spheres
### small_sphere_radius_array is the array of the radii of small spheres
### small_sphere_mass_array is the array of the masses of small spheres
### particle_position_array is the array of the positions of particles inside the Big Sphere
### particle_mass_array is the array of the masses of particles inside the Big Sphere

for small_sphere_index in np.arange(0, 10)):

    small_sphere_mass_array = []    
    small_sphere_mass = 0

    for particle_index in np.arange(0, 6000000)):           
        distances = euclidean(particle_position_array[particle_index], small_sphere_position_array[small_sphere_index])

        if (distances <= small_shpere_radius_array[small_sphere_index]):
            part_particle=particle_mass_array[particle_index]
            small_sphere_mass +=  part_particle          
            small_sphere_mass_array.append(part_particle)


    print('{}, {}'.format(small_sphere_id_array[small_sphere_index], small_sphere_mass))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42463767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档