Python将regex结果传递给lis

2024-10-02 22:35:41 发布

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

我试图将regex findall()函数的结果传递给一个列表。我使用的代码如下:

#iterate DoL, find fobIDs and use to find edgeloop IDs
for key, val in DoL.items():
    fobId = val["FaceOuterBound"]
    edgeloop_txt = re.findall(r'\n#'+str(fobId)+r'\D*#(\d+).*;', text)
    edgeloops = [int(edgeloop) for edgeloop in edgeloop_txt]
    print(edgeloops)

for循环在字典中迭代,每次都更改fobId,每次都生成不同的匹配。当前输出如下所示:

[159]
[328]
[37]
[18]
...

但是,我希望它看起来像这样:

[159, 328, 37, 18,....]

我猜这与for循环每次更改变量edgeloop_txt有关,但我不确定如何避免这种情况


Tags: 函数代码intxt列表forvalfind
1条回答
网友
1楼 · 发布于 2024-10-02 22:35:41

现在,您正在创建一个新数组,并且每次通过循环打印它。尝试以下操作:

total_array = []

for key, val in DoL.items():
    fobId = val["FaceOuterBound"]
    edgeloop_txt = re.findall(r'\n#'+str(fobId)+r'\D*#(\d+).*;', text)
    total_array += [int(edgeloop) for edgeloop in edgeloop_txt]
print(total_array)

这将循环您的项目,不断地添加到total_array,并在循环完成时打印整个数组

相关问题 更多 >