我试图在一个字符串中找到所有“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]
我尝试将其放入数组并像这样附加输出,但结果是没有
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
任何帮助都将不胜感激
您正在打印
append
的输出(它不输出任何内容,因此None
),而不是您想要的spots
相关问题 更多 >
编程相关推荐