【问题标题】:Make operations in loop between instances in Python在 Python 中的实例之间循环进行操作
【发布时间】:2020-12-03 17:33:11
【问题描述】:

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

#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) 值的元组。

提前致谢!

【问题讨论】:

    标签: python list loops class instance


    【解决方案1】:

    使用itertools 来获取球体的组合。

    只需遍历这些对并测量距离

    例子:

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

    输出

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

    【讨论】:

      【解决方案2】:

      根据您现有的代码,您似乎想创建一个行星列表,然后使用行为方法来显示与其他行星的距离。对于列表,您可以使用类级别变量,然后在 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)
      

      【讨论】:

      • 非常感谢!我头疼的太多了:)
      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2020-12-12
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      相关资源
      最近更新 更多