Re: [問題] 關於index

看板Python作者 (S‧無限)時間15年前 (2009/10/28 10:27), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《gkofay (歐歐正夯)》之銘言: : st="application" : for i in st: : print(st.index(i)) : 我希望出現的值是 : 0 : 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 可是出現的結果是 : 0 a : 1 p : 1 p : 3 l : 4 i : 5 c : 0 a : 7 t : 4 i : 9 o : 10 n : 重複到字母的索引會變成最剛出現的那個索引位置 : 我要怎要才能讓他按照順序顯示呢?? : 麻煩了 小弟獻醜了,參考一下 st = "application" lastfound = {} for i in st: lastfound[i] = st.index(i, lastfound.get(i, -1)+1) print '%-2s %s' % (lastfound[i], i) 輸出: 0 a 1 p 2 p 3 l 4 i 5 c 6 a 7 t 8 i 9 o 10 n 原文有大大提示可以用內建的enumerate 的確簡單多了,比我上面那樣土砲好XD st = "application" for i in enumerate(st): print '%-2s %s' % i 輸出相同 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.24.149 ※ 編輯: SMUGEN 來自: 114.32.24.149 (10/28 10:36)
文章代碼(AID): #1AvwmaYs (Python)
討論串 (同標題文章)
本文引述了以下文章的的內容:
0
1
完整討論串 (本文為第 2 之 2 篇):
0
1
文章代碼(AID): #1AvwmaYs (Python)