1

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

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
Then do the code to save the file

Then start the loop again at 10

Any help would be great.

2 Answers 2

5

Assuming I've understood your question correctly, you could use an 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 Docs

Sign up to request clarification or add additional context in comments.

Comments

0

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 

1 Comment

for cnt, i in enumerate(listOfSteps): would achieve the same thing without manually counting up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.