使操作在Python中的实例之间循环

问题描述

我正在尝试制作一个简单的程序,其中每个实例必须循环执行一些操作:

#this is in a class 
class Sphere(Simulation):
    spheres = []
    def __init__(self,name,mass,ray,position):
        self.name = name
        self.mass = mass
        self.ray = ray
        self.position = position
        self.behaviour()

    def gravity(self):
        global G
        G = 0.0000000000667408
        gravity_force = G * self.mass / self.ray / self.ray
        return gravity_force

    def behaviour(self):
        for sphere in Sphere.spheres:
            distance_x = np.sqrt(np.square(self.position[0] - sphere.position[0]))  #need to call a planet
            distance_y = np.sqrt(np.square(self.position[1] - sphere.position[1]))
            distance_z = np.sqrt(np.square(self.position[2] - sphere.position[2]))
            distance = (distance_x,distance_y,distance_z)
            print("distance from " + self.name + str(distance))

earth = Sphere("earth",5972000000000000000000000,6371000,(2,0))
moon = Sphere("moon",73420000000000000000000,1737100,(-2,0))
mars = Sphere("mars",639000000000000000000000,3389500,(1,3,2))
earth.gravity()
moon.gravity()
mars.gravity()

问题是:我希望每个行星都必须测量彼此之间的距离。因此,例如对于地球,它将测量距月球和火星的距离。我不知道该怎么做。距离应为(x,y,z)值的元组。

谢谢

解决方法

使用itertools顺序获得球体的组合。

只需在两对之间循环并测量距离

示例:

import itertools

print(list(itertools.combinations([1,2,3],2)))

输出

[(1,2),(1,3),(2,3)]
,

根据您现有的代码,您似乎想要创建一个行星列表,然后使用行为方法显示距其他行星的距离。对于列表,可以使用类级别变量,然后将每个行星附加在init方法中。

这是工作代码:

import numpy as np

#this is in a class 
class Sphere():  #Simulation):
    spheres = []   # class variable to hold all planets
    def __init__(self,name,mass,ray,position):
        self.name = name
        self.mass = mass
        self.ray = ray
        self.position = position
        #self.behaviour()
        Sphere.spheres.append(self)  # add this planet to list

    def gravity(self):
        global G
        G = 0.0000000000667408
        gravity_force = G * self.mass / self.ray / self.ray
        return gravity_force

    def behaviour(self):
        for sphere in Sphere.spheres:  # each planet in list
            if (sphere == self): continue  # skip this this planet (always 0,0)
            distance_x = np.sqrt(np.square(self.position[0] - sphere.position[0]))  #need to call a planet
            distance_y = np.sqrt(np.square(self.position[1] - sphere.position[1]))
            distance_z = np.sqrt(np.square(self.position[2] - sphere.position[2]))
            distance = (distance_x,distance_y,distance_z)
            print(self.name,"distance from " + sphere.name +"  " + str(distance))

earth = Sphere("earth",5972000000000000000000000,6371000,0))
moon = Sphere("moon",73420000000000000000000,1737100,(-2,0))
mars = Sphere("mars",639000000000000000000000,3389500,3,2))
earth.gravity()
moon.gravity()
mars.gravity()

# print distances
earth.behaviour()
moon.behaviour()
mars.behaviour()

输出

earth distance from moon  (4.0,0.0,0.0)
earth distance from mars  (1.0,3.0,2.0)
moon distance from earth  (4.0,0.0)
moon distance from mars  (3.0,2.0)
mars distance from earth  (1.0,2.0)
mars distance from moon  (3.0,2.0)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...