在 Python 中的实例之间进行循环操作
Make operations in loop between instances in 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, 0))
moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 3, 2))
earth.gravity()
moon.gravity()
mars.gravity()
问题是:我希望每个行星都必须测量彼此之间的距离。因此,例如对于地球,它将测量与月球和火星的距离。我不知道如何循环执行。距离应该是 (x, y, z) 值的元组。
提前致谢!
使用 itertools 以获得 Spheres 的组合。
只需遍历对并测量距离
示例:
import itertools
print(list(itertools.combinations([1, 2, 3], 2)))
输出
[(1, 2), (1, 3), (2, 3)]
根据您现有的代码,您似乎想要创建一个行星列表,然后使用行为方法显示与其他行星的距离。对于列表,您可以使用 class 级别变量,然后在 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,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, (2, 0, 0))
moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 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, 0.0)
moon distance from mars (3.0, 3.0, 2.0)
mars distance from earth (1.0, 3.0, 2.0)
mars distance from moon (3.0, 3.0, 2.0)
我正在尝试制作一个简单的程序,其中每个实例都必须在循环中进行一些操作:
#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, 0))
moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 3, 2))
earth.gravity()
moon.gravity()
mars.gravity()
问题是:我希望每个行星都必须测量彼此之间的距离。因此,例如对于地球,它将测量与月球和火星的距离。我不知道如何循环执行。距离应该是 (x, y, z) 值的元组。
提前致谢!
使用 itertools 以获得 Spheres 的组合。
只需遍历对并测量距离
示例:
import itertools
print(list(itertools.combinations([1, 2, 3], 2)))
输出
[(1, 2), (1, 3), (2, 3)]
根据您现有的代码,您似乎想要创建一个行星列表,然后使用行为方法显示与其他行星的距离。对于列表,您可以使用 class 级别变量,然后在 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,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, (2, 0, 0))
moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 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, 0.0)
moon distance from mars (3.0, 3.0, 2.0)
mars distance from earth (1.0, 3.0, 2.0)
mars distance from moon (3.0, 3.0, 2.0)