如何创建 numpy 数组的 i 个副本并为范围内的 i 以不同的方式命名它们?

How to create i copies of a numpy array and name them differently for i in a range?

我有一个数组 (oned_2018),我想要一个函数来制作这个数组的多个副本。副本数是这个数组中的最大值。例如最大的是 6;那么应该有6份。 谁能帮我写这个循环? 我想要类似...

for i in range(1, max(oned_2018)+1):
    classi_20 = oned_2018.copy() # of course this line is incorrect!

并且输出应该像这样的手动工作:

class1_20 = oned_2018.copy()

class2_20 = oned_2018.copy()

class3_20 = oned_2018.copy()

class4_20 = oned_2018.copy()

class5_20 = oned_2018.copy()

class6_20 = oned_2018.copy()

如果你真的想这样做,你可以使用globals():

oned_2018 = [1, 2, 3, 4]
for i in range(1, max(oned_2018)+1):
    globals()[f"class{i}_20"] = oned_2018.copy() # of course this line is incorrect!

print(class1_20) # [1, 2, 3, 4]

但通常会避免像这样动态创建变量。通常人们会建议改用 dict:

oned_2018 = [1, 2, 3, 4]
data = {f"class{i+1}_20": oned_2018.copy() for i in range(max(oned_2018))}

print(data['class1_20']) # [1, 2, 3, 4]

与其通过变量命名它们,我建议您使用这样的字典:

classes_to_data = {}
for i in range(1, max(oned_2018)+1):
    classes_to_data[i] = oned_2018.copy()

然后,您可以按如下方式访问它们:

classes_to_data[1]  # outputs what you named 'class1_20'
classes_to_data[2]  # outputs what you named 'class2_20'
# and so on...