我需要一种方法来更改 for 循环内字符串的元素,特别是我有这个:
N = 15
time_steps = np.linspace(0.005, 0.5, N)[:,None]
for ii in time_steps:
filter_results_file_name = 'augmented_filter_all_forces_ii.mat'
Run Code Online (Sandbox Code Playgroud)
特别是,我希望字符串“augmented_filter_all_forces_ii.mat”在索引 ii 的每次迭代中更改名称,然后对于 ii=0,我想要“augmented_filter_all_forces_0.mat”,对于 ii=1,我想要“augmented_filter_all_forces_1.mat”,依此类推等等。
小智 5
您可以简单地在 python 中使用“f-string”,例如:
for ii in time_steps:
filter_results_file_name = f'augmented_filter_all_forces_{ii}.mat'
Run Code Online (Sandbox Code Playgroud)