我試圖在字符串中找到所有“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)
添加回答
舉報
0/150
提交
取消