簡體   English   中英

有沒有辦法在 Python 的 for 循環中創建多個相等的 class 對象?

[英]Is there any way to create a number of equal class objects in a for loop in Python?

大約一個月前,我開始學習 Python,因為我在大學的一個項目中需要它,所以對於我在解釋中可能做出的任何概念錯誤或錯誤假設,我提前道歉。

在解釋我的問題之前,我檢查了以下幾個問題,但我認為答案不是我需要的:

Python 在for循環中創建對象

該程序模擬了許多流體粒子的軌跡。 我這樣做的方法是創建一個“線”和一個“點”object,它們將存儲粒子的 x 和 y 坐標值,然后使用 matplotlib 對其進行動畫處理:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import math

# initializing the figure in 
# which the graph will be plotted
fig = plt.figure() 
   
# setting limits for both the x-axis and y-axis
axis = plt.axes(xlim =(0, 20), 
                ylim =(-2, 50))

# initializing a line variable for each
# trajectory to be plotted
line1, = axis.plot([], [], lw = 1)
line2, = axis.plot([], [], lw = 1)

redDot, = plt.plot([1], [10], 'ro', markersize = 2)

# data which the line will 
# contain (x, y). Lines are
# initially empty objects
def init(): 
    line1.set_data([], [])
    line2.set_data([], [])
    return line1, line2
   
# initializing empty values
# for x and y coordinates
xdata1, ydata1 = [], [] 
xdata2, ydata2 = [], []

# animation function 
def animate(i): 
    # t is a parameter which varies
    # with the frame number
    t = 0.1 * i 
       
    # x, y values to be plotted 
    x = math.exp(t) 
    y = 1 + (2/3)*t**(3/2) 
    
    # appending values to the previously 
    # empty x and y data holders 
    xdata1.append(x) 
    ydata1.append(y) 
    line1.set_data(xdata1, ydata1) 
    
    x = math.exp(t) 
    y = 10 + (2/3)*t**(3/2) 
    
    # appending values to the previously 
    # empty x and y data holders 
    xdata2.append(x) 
    ydata2.append(y) 
    line2.set_data(xdata2, ydata2)
    
    redDot.set_data(x, y)
    
    return line1, line2, redDot

anim = FuncAnimation(fig, animate, init_func = init,
                     frames = 200, interval = 20, blit = True)

上面的代碼創建了一行line1, = axis.plot([], [], lw = 1) 緊接着,它會創建一個帶有redDot, = plt.plot([1], [10], 'ro', markersize = 2)的點,它將跟隨這條線。 問題是我打算同時模擬 10 個粒子,並且我相信必須有一種更清潔、更有效的方法來創建所有這些對象,而不是必須將這兩行代碼重復 10 次。 更重要的是,有兩個列表作為每個粒子的數據持有者, animate function 也需要通過所有這些列表 go 來分配計算值。 這將產生一個非常長的代碼,如果我想為更多的粒子設置動畫,那將是完全不切實際的。

有沒有辦法用循環來做到這一點? 就像是:

for i in list(range(number_of_particles)):

    #number_of_particles lines and dots are created here

我想知道是否可以將這些對象存儲在列表中,例如:

my_list = []

for i in list(range(number_of_particles)):

    my_list.append(axis.plot([], [], lw = 1))

然后我只需要在另一個 for 循環中調用所述列表的項目。

同樣,我只是一個新手,我非常感謝任何可能想要提供幫助的人的任何意見。 謝謝!

我認為您自己的建議或多或少有效,我剛剛在下面進行了調整。

  • 創建Matplotlib.lines.Line2D實例的列表
number_of_particles = 10
lines = [axis.plot([], [], lw=1)[0] for i in range(number_of_particles)]
  • 存儲每行的數據:創建一個列表來存儲每行的 x 和 y 數據,您可以執行以下操作,這只是創建一個元組列表,其中每個元組包含一個 x 坐標列表和一個用於y 坐標。 這類似於您的xdata1, ydata1 = [], []的精神,但只是此類元組的列表。
lines_data = [([], []) for i in range(number_of_particles)]
  • 更新animate function 中的每一行。 這里內置的enumerate function 為您提供第i次迭代的索引,以便您可以在循環中使用它,以及可迭代列表中的項目。 我想在函數中具有更大的靈活性,您可以將常量添加到列表中,然后在需要時用i索引它們。
for i, line in enumerate(lines):
    x = math.exp(t)
    y = i + (2/3)*t**(3/2)
    lines_data[i][0].append(x)
    lines_data[i][1].append(y)
    line.set_data(lines_data[i][0], lines_data[i][1])
  • 返回列表的聰明方法。 返回時使用*將列表解壓縮。 剛剛將lines列表添加到現有的 return 語句中。
return line1, line2, redDot, *lines

最后,我不完全確定評論

更重要的是,有兩個列表充當每個粒子的數據持有者,動畫 function 也需要通過所有這些列表 go 來分配計算值。

也許您可以詳細說明,但似乎新的計算只是附加到最后,我認為這不應該產生最壞的影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM