为了账号安全,请及时绑定邮箱和手机立即绑定

将 finditer() 输出放入数组中

将 finditer() 输出放入数组中

一只萌萌小番薯 2022-08-02 11:00:00
我试图在字符串中找到所有“TRN”的索引,我已经这样做了,但是我想把所有的索引放在一个数组中,我似乎做不到。    import re    string = 'a string with TRN, then another TRN'    for match in re.finditer('TRN', string):        spots = match.start()        print(spots)输出为:14  32  我想要的输出是:[14,32]我尝试将其放入数组并像这样附加输出,但结果是 NONE NONE。    import re    into_array = []    string = 'a string with TRN, then another TRN'    for match in re.finditer('TRN', string):        spots = match.start()        x = into_array.append(spots)        print(x)输出为:None  None  任何帮助将不胜感激。
查看完整描述

1 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

您正在打印 的输出(因此不会输出任何内容),而不是您想要的那样。appendNonespots


import re


into_array = []


string = 'a string with TRN, then another TRN'


for match in re.finditer('TRN', string):

    spots = match.start()

    into_array.append(spots)

print(into_array)


查看完整回答
反对 回复 2022-08-02
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信