简体   繁体   中英

How can I define a loop in a command?

I am writing a code for ABAQUS software by python and I need to write below code in a section of my code.

a1.InstanceFromBooleanMerge(name='agg', instances=(a1.instances['Part-1-1'], 
   a1.instances['Part-2-1'], ), keepIntersections=ON, 
   originalInstances=DELETE, domain=GEOMETRY)

In aforementioned code, The number of Part will be varied and I do not know how many part I have before running the code.

So,for example if I have 3 Parts, how can I adjust my code? in this case, code has to be same as followings;

a1.InstanceFromBooleanMerge(name='agg', instances=(a1.instances['Part-1-1'], 
    a1.instances['Part-2-1'], a1.instances['Part-3-1'],),                   
    keepIntersections=ON, originalInstances=DELETE, domain=GEOMETRY)

As you can see, this is a command and I do not know how I have to define something such For loop in a command???

you can use a list comprehension to build up the list "within" the method call:

a1.InstanceFromBooleanMerge(
    name='agg', 
    instances=tuple([a1.instances["Part-%s-1" % i] for i in range(1,4)]),                   
    keepIntersections=ON,
    originalInstances=DELETE,
    domain=GEOMETRY)

where 4 is the length of the matrix you get plus 1, eg range(1, len(matrix)+1)

Another way would be to build up the tuple outside the method call:

instances = tuple([a1.instances["Part-%s-1" % i] for i in range(1,4)])
a1.InstanceFromBooleanMerge(
    name='agg', 
    instances=instances,
    keepIntersections=ON,
    originalInstances=DELETE,
    domain=GEOMETRY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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