如何遍历一个集合并得到s的最后一个值

2024-04-18 23:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想遍历一个名为Group的集合并得到组的最后一个值。在

我尝试了以下代码:

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

我的结果如下:

^{pr2}$

在上面的代码中,我使用了一个范围(1,6)作为示例,但实际上我不知道范围号,所以我希望编写不使用range/xrange或{}的代码。在

尽管代码是用Python编写的,但我的问题更一般。在


Tags: 代码in示例forbasegrouprangelist
3条回答

你的代码没什么意义。在

m.append(set)

不执行任何操作,它只是将python类类型set附加到m,而不是从base.Getentity获得的值

但回到问题上来。在

你可以尝试使用while循环。在

是这样的:

^{pr2}$
my_set = {1, 'hello', 12.4}
print(my_set)
print(list(my_set).pop())

--output:--
{1, 12.4, 'hello'}
hello

^{pr2}$

我这样简单的迭代解决方案可能可以完成您的工作:

last_group = None
i = 0
while True:
    next_group = base.Getentity(constants.ABAQUS, "SET", i)
    i += 1
    if next_group is None:
        break
    last_group = i, next_group

print last_group

相关问题 更多 >