在这段代码中,我试图使用np.random.shuffle()
打乱数组r2
,并在每次运行时将其以CSV
格式保存在多个文件夹中。但它实际上并没有洗牌。我不知道为什么。
import os
import csv
import numpy as np
Runs=2
r2=np.array([[54.3817864 , 53.42810469, 47.94544424, 53.63511598, 49.59728558,
49.6935623 , 50.70031442, 52.80854701, 50.18808714, 51.80017684,
51.85747597, 54.3817864 ]])
def function(run):
parent_folder = str(run + 1)
os.mkdir(parent_folder)
with open(os.path.join(parent_folder, rf"Inv_Radius_220ND.csv"), 'w+') as f:
np.random.shuffle(r2)
print(f'{run}:{r2}')
writer = csv.writer(f)
writer.writerows(r2)
for x in range(Runs):
function(x)
当前输出为:
0:[[54.3817864 53.42810469 47.94544424 53.63511598 49.59728558 49.6935623
50.70031442 52.80854701 50.18808714 51.80017684 51.85747597 54.3817864 ]]
1:[[54.3817864 53.42810469 47.94544424 53.63511598 49.59728558 49.6935623
50.70031442 52.80854701 50.18808714 51.80017684 51.85747597 54.3817864 ]]
np.random.shuffle(r2)
正在打乱二维数组的第一个维度。由于第一维度只包含一个数组,因此没有什么可混洗的。
这应该做你想做的:
>>> r2
array([[54.3817864 , 53.42810469, 47.94544424, 53.63511598, 49.59728558,
49.6935623 , 50.70031442, 52.80854701, 50.18808714, 51.80017684,
51.85747597, 54.3817864 ]])
>>> np.random.shuffle(r2[0])
>>> r2
array([[47.94544424, 54.3817864 , 50.18808714, 49.6935623 , 49.59728558,
50.70031442, 52.80854701, 51.85747597, 53.42810469, 53.63511598,
54.3817864 , 51.80017684]])