Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套用于终端中的循环_Python_Bash_Shell - Fatal编程技术网

Python 嵌套用于终端中的循环

Python 嵌套用于终端中的循环,python,bash,shell,Python,Bash,Shell,我有一个包含31个不同文件的目录,每个文件代表一种单元格类型(下面显示了其中的一个示例) 我有一个脚本,为每个单元格类型的文件生成22个新文件(因此31*22=682个文件) 这适用于每个文件,但我不想在每次脚本完成时更改命令中的名称。相反,我希望对目录中的每个文件运行此命令 有办法做到这一点吗?这里有一个可能的解决方案,使用嵌套for循环: # Iterate over all GeneSet files for f in *.GeneSet; do # Figure out the

我有一个包含31个不同文件的目录,每个文件代表一种单元格类型(下面显示了其中的一个示例)

我有一个脚本,为每个单元格类型的文件生成22个新文件(因此31*22=682个文件)

这适用于每个文件,但我不想在每次脚本完成时更改命令中的名称。相反,我希望对目录中的每个文件运行此命令


有办法做到这一点吗?

这里有一个可能的解决方案,使用嵌套for循环:

# Iterate over all GeneSet files
for f in *.GeneSet; do
    # Figure out the cell type by removing "Genes.GeneSet" from the end of the filename
    cell_type=${f%Genes.GeneSet}
    # Process the file
    echo "Processing file: $f with cell type $cell_type"
    for i in {1..22}; do
        python make_annot.py --gene-set-file "$f"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file ${cell_type}.${i}.annot.gz
    done
done 

下面是一个使用嵌套for循环的可能解决方案:

# Iterate over all GeneSet files
for f in *.GeneSet; do
    # Figure out the cell type by removing "Genes.GeneSet" from the end of the filename
    cell_type=${f%Genes.GeneSet}
    # Process the file
    echo "Processing file: $f with cell type $cell_type"
    for i in {1..22}; do
        python make_annot.py --gene-set-file "$f"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file ${cell_type}.${i}.annot.gz
    done
done 

您可以对循环使用以下嵌套的

for f in *Genes.GeneSet 
do 
    for i in `seq 1 22`
    do 
        python make_annot.py --gene-set-file "${f}"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file `echo "${f}" | grep -oP '^.*(?=Genes.Geneset$)'`.${i}.annot.gz; 
    done
done
这将在所有
CELL\u-TYPEGenes.GeneSet
CELL\u-TYPE.${i}.annot.gz
上循环,以生成
31*22=682个文件的组合


其中
echo“${f}”|grep-oP'^.*(=Genes.Geneset$)”
用于获取文件名的单元格类型部分

您可以使用以下嵌套的
进行
循环:

for f in *Genes.GeneSet 
do 
    for i in `seq 1 22`
    do 
        python make_annot.py --gene-set-file "${f}"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file `echo "${f}" | grep -oP '^.*(?=Genes.Geneset$)'`.${i}.annot.gz; 
    done
done
这将在所有
CELL\u-TYPEGenes.GeneSet
CELL\u-TYPE.${i}.annot.gz
上循环,以生成
31*22=682个文件的组合


其中
echo“${f}”| grep-oP'^.*(=Genes.Geneset$)”
用于获取文件名的单元格类型部分

每个输入的预期输出是什么?一组22个名为CELL_-TYPE.chr#.annot.gz的文件,其中单元格类型用于目录中的每个单元格,chr#是一个从1到22的数字。
make_annot.py
是否有类似于帮助标志(
-h/--help
)的东西?如果是这样的话,您最好在这里发布帮助页面。每个输入的预期输出是什么?一组名为CELL_TYPE.chr#.annot.gz的22个文件,其中CELL_TYPE代表目录中的每个单元格,chr#是1-22之间的数字。
make_annot.py
是否有类似于帮助标志(
-h/-help
)的内容?如果有,请将帮助页张贴在此处。@erip:Edited,因为它实际上完全没有必要;-)谢谢你的反馈@erip:已编辑,因为它实际上完全没有必要;-)谢谢你的反馈!这很有效。感谢您的明确回答,现在我知道如何在bash中编写嵌套循环了!内部for循环可以更有效地编写为
for((i=1;我使用该方法的速度比你快得多(或者你有没有更快的源代码)?这似乎是合理的(因为它不需要外部命令),但在我使用seq(
time for i in
seq 1 100000
;do:;done
)大约和使用大括号扩展(
time for i in{1..100000};do:;done
)一样快,而使用循环检查(
time for)((i=1;i)效果很好。感谢您的明确回答,现在我知道如何在bash中编写嵌套循环了!内部for循环可以像
for>那样更有效地编写((i=1;我使用该方法的速度比你快得多(或者你有更快的消息来源吗)?这似乎是合理的(因为它不需要外部命令),但在我的测试中,使用seq(
time for i in
seq 1 100000
;do:;done
)大约和使用大括号扩展(
time for i in)一样快{1..100000};do:;done
),而使用循环检查(
时间((i=1;i