简体   繁体   English

在Python清单中循环,每个循环使用多个项目

[英]Loop in Python List using more than one item per loop

So I'm writing a script for a program called Abaqus.... and I have a list of numbers, and I need to loop through the numbers in the following manner 因此,我正在为名为Abaqus的程序编写脚本。...,我有一个数字列表,我需要按以下方式遍历数字

listOfSteps = [1, 4, 7, 10, 17, 22, 28, 29, 30, 43, 47, 50]
fileNameCreate = 0 

for i in listOfSteps:

    session.viewports['Viewport: 1'].odbDisplay.setFrame(step=i, frame=-1)
    session.viewports['Viewport: 2'].odbDisplay.setFrame(step=i, frame=-1)
    session.viewports['Viewport: 3'].odbDisplay.setFrame(step=i, frame=-1)
    session.printOptions.setValues(reduceColors=False)
    session.printToFile(fileName='C:/Image'+str(fileNameCreate+1), format=PNG, 
        canvasObjects=(session.viewports['Viewport: 3'], 
        session.viewports['Viewport: 2'], session.viewports['Viewport: 1']))

So I need the first step to use 1, 2nd step to use 4, 3rd step to use 7 所以我需要第一步使用1,第二步使用4,第三步使用7
Then do the code to save the file 然后执行代码以保存文件

Then start the loop again at 10 然后在10重新开始循环

Any help would be great. 任何帮助都会很棒。

Assuming I've understood your question correctly, you could use an iterator : 假设我正确理解了您的问题,则可以使用iterator

listOfSteps = [1, 4, 7, 10, 17, 22, 28, 29, 30, 43, 47, 50]
fileNameCreate = 0 

it = iter(listOfSteps)

for a in it:
    b = next(it)
    c = next(it)

    session.viewports['Viewport: 1'].odbDisplay.setFrame(step=a, frame=-1)
    session.viewports['Viewport: 2'].odbDisplay.setFrame(step=b, frame=-1)
    session.viewports['Viewport: 3'].odbDisplay.setFrame(step=c, frame=-1)

    # ...

And, if you don't mind a little bit of magic: 而且,如果您不介意一点魔术的话:

for a, b, c in zip(*[iter(listOfSteps)]*3):
    # ...

The left-to-right evaluation order of the iterables is guaranteed. 保证了可迭代对象的从左到右的评估顺序。 This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n) . 这使得使用zip(*[iter(s)]*n)将数据系列聚类为n个长度的组成为可能。 ~ Zip Docs 〜邮编文档

Maybe try to add a counter variable to your code 也许尝试在您的代码中添加一个计数器变量

listOfSteps = [1, 4, 7, 10, 17, 22, 28, 29, 30, 43, 47, 50]
fileNameCreate = 0 
cnt = 0

for i in listOfSteps:
   cnt =+ 1
   if cnt % 3 == 0:
       # here do your write magic 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM