简体   繁体   中英

how to loop through and get the last value

Hi i have the following code:

m= list()
for i in range (1,6):
    set = base.Getentity(constants.ABAQUS,"SET",i)
    m.append(set)
    print(set)

and my result is

<Entity:0*17a:id:1>
<Entity:0*14g:id:2>
<Entity:0*14f:id:3>
<Entity:0*14a:id:4>
None
None

Here i have four elemnts in my set named set. Even though my code is written in ansa python, my question is very General

I would like to write a code which goes through the set and prints the last elemnt in my case

'<Entity:0*17a:id:4>'.

and aslo i dont want to use the range function so pls help me with writing the code.

我建议你看看Iterators ,它会帮助你遍历列表

If you don't want to use the range function, you can use xrange. It returns an xrange object, which is kind of like an iterator and generates the numbers on demand.

You are getting None as the last two values because there are no 'set' with the id 5 and 6 in your model Use a filter before appending to the list m

m= list()
for i in range (1,6)
    set = base.Getentity(constants.ABAQUS,"SET",i)
    if set!=None:
        m.append(set)

Now you can just call m[-1] for the last entity

hope this helps

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