gpt4 book ai didi

python - 在 For 循环中从 Windows 批处理文件中的数组访问项目

转载 作者:可可西里 更新时间:2023-11-01 11:43:51 25 4
gpt4 key购买 nike

我试图遍历 Windows 批处理文件中的列表,使用不同列表中的不同数字作为每次迭代中的参数。

set input_image_list=(1 2 3 4 5 6 7 8 9 )

set output_image_list = (2 3 4 5 6 7 8 9 10)

set next_needed_image = (002 003 004 005 006 007 008 009 010)

FOR %%A IN (2 3 4 5 6 7 8 9 10) DO python batchLayer4ThirdVideo.py --input_image !input_image_list[%%A]! --next_undreamed_image !next_needed_image[%%A]! --output_image %%A

但是当我将它们用作参数时,列表的索引不起作用,它只是将索引(例如 input_image_list[2])作为我的参数,而它应该只是一个数字。

我怎样才能正确索引列表,以便其中的每个数字都作为参数传递?

最佳答案

有一个(不知何故令人费解)technique将列表元素之间的空格(或任何其他字符)替换为计数和集合以形成数组,从而将字符串列表扩展为数组。

从您的列表和 for 中不清楚您是否想要基于 0 或 1 的索引。
(更简单的方法是 for/l %%A in (2,1,10) Do ...)

为了更好地理解,我使用了较短的变量名并将拆分后的 python 命令回显到多行。

:: Q:\Test\2018\07\05\SO_51191502.cmd
@Echo off & SetLocal EnableDelayedExpansion

set i=0&Set "ImgIn= 1 2 3 4 5 6 7 8 9 10"
Set "ImgIn=%ImgIn: ="&Set /a i+=1&Set "ImgIn[!i!]=%"
:: Set ImgIn

set i=0&Set "ImgOut= 2 3 4 5 6 7 8 9 10 11"
Set "ImgOut=%ImgOut: ="&Set /a i+=1&Set "ImgOut[!i!]=%"
:: Set ImgOut

set i=0&Set "ImgNext= 002 003 004 005 006 007 008 009 010 011"
Set "ImgNext=%ImgNext: ="&Set /a i+=1&Set "ImgNext[!i!]=%"
:: Set ImgNext

For /L %%A IN (1,1,10) DO (
Echo python batchLayer4ThirdVideo.py ^
--input_image !ImgIn[%%A]! ^
--next_undreamed_image !ImgNext[%%A]! ^
--output_image !ImgOut[%%A]!
)

示例输出:

> Q:\Test\2018\07\05\SO_51191502.cmd
python batchLayer4ThirdVideo.py --input_image 1 --next_undreamed_image 002 --output_image 2
python batchLayer4ThirdVideo.py --input_image 2 --next_undreamed_image 003 --output_image 3
python batchLayer4ThirdVideo.py --input_image 3 --next_undreamed_image 004 --output_image 4
python batchLayer4ThirdVideo.py --input_image 4 --next_undreamed_image 005 --output_image 5
python batchLayer4ThirdVideo.py --input_image 5 --next_undreamed_image 006 --output_image 6
python batchLayer4ThirdVideo.py --input_image 6 --next_undreamed_image 007 --output_image 7
python batchLayer4ThirdVideo.py --input_image 7 --next_undreamed_image 008 --output_image 8
python batchLayer4ThirdVideo.py --input_image 8 --next_undreamed_image 009 --output_image 9
python batchLayer4ThirdVideo.py --input_image 9 --next_undreamed_image 010 --output_image 10
python batchLayer4ThirdVideo.py --input_image 10 --next_undreamed_image 011 --output_image 11

关于python - 在 For 循环中从 Windows 批处理文件中的数组访问项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191502/

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