这有点难以解释,但我使用的目录有许多不同的文件但基本上我想循环不规则间隔的文件
所以在伪代码中我猜它会写成:
A = 1E4, 1E5, 5E5, 7E5, 1E6, 1.05E6, 1.1E6, 1.2E6, 1.5E6, 2E6 For A in range(start(A),end(A)): inputdir ="../../../COMBI_Output/Noise Studies/[A] Macro Particles/10KT_[A]MP_IP1hoN0.0025/"运行其余代码
因为目前我正在通过改变[A]的值来手动完成它,这是一个噩梦和耗时。 我在macbook上使用Python ,所以我想知道编写一个在Python调用的bash脚本是否是正确的想法?
或者用文本文件替换A ,以便:
import numpy as np mpnum=np.loadtxt("mp.txt") for A in range(0,len(A)): for B in range(0,len(A)): inputdir ="../../../COMBI_Output/Noise Studies/",[A] "Macro Particles/10KT_",[A]"MP_IP1hoN0.0025/"但我先尝试了这个,但仍然没有运气。
It's kind of hard to explain but I'm using a directory that has a number of different files but essentially I want to loop over files with irregular intervals
so in pseudocode I guess it would be written like:
A = 1E4, 1E5, 5E5, 7E5, 1E6, 1.05E6, 1.1E6, 1.2E6, 1.5E6, 2E6 For A in range(start(A),end(A)): inputdir ="../../../COMBI_Output/Noise Studies/[A] Macro Particles/10KT_[A]MP_IP1hoN0.0025/"Run rest of code
Because at the moment I'm doing it manually by changing the value in [A] and its a nightmare and time consuming. I'm using Python on a macbook so I wonder if writing a bash script that is called within Python would be the right idea?
Or replacing A with a text file, such that its:
import numpy as np mpnum=np.loadtxt("mp.txt") for A in range(0,len(A)): for B in range(0,len(A)): inputdir ="../../../COMBI_Output/Noise Studies/",[A] "Macro Particles/10KT_",[A]"MP_IP1hoN0.0025/"But I tried this first and still had no luck.
最满意答案
你快到了。 您不需要范围,只需遍历列表即可。 然后使用格式替换字符串。
A = ['1E4', '1E5', '5E5', '7E5', '1E6', '1.05E6', '1.1E6', '1.2E6', '1.5E6', '2E6'] for a in A: inputdir = "../../../COMBI_Output/Noise Studies/{} Macro Particles/10KT_{}MP_IP1hoN0.0025/".format(a)You are almost there. You don't need a range, just iterate over the list. Then do a replacement in the string using format.
A = ['1E4', '1E5', '5E5', '7E5', '1E6', '1.05E6', '1.1E6', '1.2E6', '1.5E6', '2E6'] for a in A: inputdir = "../../../COMBI_Output/Noise Studies/{} Macro Particles/10KT_{}MP_IP1hoN0.0025/".format(a)更多推荐
发布评论