gpt4 book ai didi

python - GNU 并行数组参数

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:46 26 4
gpt4 key购买 nike

假设我有一个 python 文件test.py:

import os

class print_args(object):

def__init__(self, x, y, z):

self.x = x
self.y = y
self.z = z

print(x)
print(y)
print(z)

if __name__ == '__main__':

parser = argparse.ArgumentParser()
parser.add_argument('--x', nargs='+', type = str)
parser.add_argument('--y', nargs='+', type = int)
parser.add_argument('--z', type = int)
args = parser.parse_args()

print_args(args.x, args.y, args.z)

我可以使用如下参数从终端运行 test.py:

python3 test.py --x a b --y 2 3 --z 10

结果符合预期:

a b
2 3
10

如何在带有数组参数的终端中使用 GNU parallel 运行 test.py解决方案等同于运行:

python3 test.py --x a b --y 2 3 --z 10
python3 test.py --x a b --y 2 3 --z 20
python3 test.py --x a b --y 2 3 --z 30
python3 test.py --x a b --y 2 3 --z 40

我在回答我自己的问题时的错误尝试是:

parallel --link 'python3 test.py --x {1} --y {2} --z {3}' ::: \
> 'a b' 'a b' 'a b' 'a b' ::: \
> '2 3' '2 3' '2 3' '2 3' ::: \
> '10' '20' '30' '40'

最佳答案

希望正如评论中所讨论的那样,这对您有用:

parallel -k --dry-run python3 test.py --x "a b" --y "2 3" --z ::: 10 20 30 40

示例输出

python3 test.py --x a b --y 2 3 --z 10
python3 test.py --x a b --y 2 3 --z 20
python3 test.py --x a b --y 2 3 --z 30
python3 test.py --x a b --y 2 3 --z 40

如果命令看起来不错,请删除 --dry-run 部分,然后再次真正运行它。

关于python - GNU 并行数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com