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

标签 python arrays windows batch-file indexing

我试图遍历 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/

相关文章:

python - 拆分分隔文件并存储到新列中

Python GTK3 : how to create a Gtk. 文件选择对话框?

Javascript 对象多重引用

c++ - 随机完整系统无响应运行数学函数

c# - 使用凭据从远程、不受信任的域访问共享文件 (UNC)

python - 在 python 中编写 "try elsetry"的最佳方法?

python - 清除 Tkinter 条目文本 - 所以新文本看起来很新?

java - QuickSort的修改(分区Hoare),先偶数降序,然后奇数降序

javascript - 如何对对象的值求和并将其放入数组中

c++ - 如何将任意大整数从任意基数转换为不同的基数?