[問題] ValueError: substring not found
我想要把所有'a'的index找出,我知道有正規畫表示法,
但在自己在試寫的時候出現了下面問題,請幫忙解惑,感謝大家!
x = 'An apple a day, keeps the doctor away'
i= 0
j = []
while x.index('a',i) is not None:
i = x.index('a',i) + 1
j.append(x.index('a',i))
print(j) #如果這邊往左移,不放在while迴圈內則會不顯示j序列,不知道為什麼@@!
----------------------------------------------------------------------------
Out:
[9]
[9, 12]
[9, 12, 33]
[9, 12, 33, 35]
Traceback (most recent call last):
File "C:/Users/cash.chien/PycharmProjects/element_number/test.py", line 7,
in <module>
j.append(x.index('a',i))
ValueError: substring not found
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.131.189
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1467692026.A.A7A.html
→
07/05 12:55, , 1F
07/05 12:55, 1F
→
07/05 13:04, , 2F
07/05 13:04, 2F
→
07/05 13:04, , 3F
07/05 13:04, 3F
→
07/05 13:18, , 4F
07/05 13:18, 4F
→
07/05 13:18, , 5F
07/05 13:18, 5F
→
07/05 13:23, , 6F
07/05 13:23, 6F
推
07/05 15:32, , 7F
07/05 15:32, 7F
→
07/05 16:37, , 8F
07/05 16:37, 8F
→
07/05 16:38, , 9F
07/05 16:38, 9F
→
07/05 20:25, , 10F
07/05 20:25, 10F
Python 近期熱門文章
PTT數位生活區 即時熱門文章