Python Forum

Full Version: Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm trying to create a code for postprocessing some FEA results by using a Python script associated with Abaqus scripting.
my current code is as follow

if Number_Fea == 1:
    xy1 = session.xyDataObjects['RF : %s' %Cases[0]]
    c1 = session.Curve(xyData=xy1)
    chart.setValues(curvesToPlot=(c1, ), )

if Number_Fea == 2:
    xy1 = session.xyDataObjects['RF : %s' %Cases[0]]
    xy2 = session.xyDataObjects['RF : %s' %Cases[1]]
    c1 = session.Curve(xyData=xy1)
    c2 = session.Curve(xyData=xy2)
    chart.setValues(curvesToPlot=(c1, c2, ), )
My question: I would like to loop through my code without repeating these lines of code for each "Number_Fea" value.
The line where I have a problem is the last one ( chart.setValues(curvesToPlot=(c1, c2, ), )). how can I add more variables (c3, c4, c5, ...) when the user gives the "Number of FEA"?
I was thinking about using a dictionary or a function but I couldn't make it work.
Thanks for your help
You could do
xy = [session.xyDataObjects['RF : %s' %Cases[k]] for k in range(Number_Fea)]
c = [session.Curve(xyData=v) for v in xy]
chart.setValues(curvesToPlot=tuple(c), )
(Feb-08-2022, 09:29 AM)Gribouillis Wrote: [ -> ]You could do
xy = [session.xyDataObjects['RF : %s' %Cases[k]] for k in range(Number_Fea)]
c = [session.Curve(xyData=v) for v in xy]
chart.setValues(curvesToPlot=tuple(c), )

Thank you a lot for your help, it works perfectly!! :)