[英]Executing a Python script multiple times with different option flags
I have a Python script using the parser
package for setting option flags (like -d dataset
and -s size
etc. and finally the results are written to a file. How can i run the command many times (sequentially) with different option flags for each run? 我有一个Python脚本使用parser
包来设置选项标志(如-d dataset
和-s size
等),最后将结果写入文件。如何使用不同的选项标志多次(顺序)运行命令每次跑?
I need something like this: 我需要这样的东西:
datasets = [a,b,c]
sizes = [100,200,300]
for dataset in dataset: #specify parameters
for size in sizes: #specify more parameters
python script.py -d dataset -s size #run script
What would be the best (or even 'a') way to do this? 这样做的最佳方式(甚至是'a')是什么?
A direct translation of your pseudo/python-code to bash
, with arrays instead of list
s and classic for
loops: 将伪/ python代码直接转换为bash
,使用数组而不是list
s和classic for
循环:
#!/bin/bash
datasets=('a' 'b' 'c')
sizes=(100 200 300)
for dataset in "${datasets[@]}"; do
for size in "${sizes[@]}"; do
python script.py -d "$dataset" -s "$size"
done
done
If sizes
are more like a range
, and not a list of hand-picked values, you can use the brace expansion : 如果sizes
更像是一个range
,而不是一个精选值列表,则可以使用大括号扩展 :
for size in {100..300..100}; do
# ...
done
or the arithmetic for
loop: 或运算for
循环:
for ((size=100; size<=300; size+=100)); do
# ...
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.