【python 字母索引】找到英文句子裡面每個單詞最後一個字母的索引
阿新 • • 發佈:2018-11-03
# -*- coding:utf-8 -*- sentence="wo ai ni zhong guo" def find_english_word_last_index(sentence): """ :param sentence: 英文句子 :return: 返回英文句子的每個單詞最後的字母的索引 """ last_index_list=[] for i,j in enumerate(sentence): print(i,j) if j==' ': last_index_list.append(i-1) last_index_list.append(len(sentence)-1) print(last_index_list) return last_index_list if __name__ == '__main__': find_english_word_last_index(sentence)
執行結果:
0 w
1 o
2
3 a
4 i
5
6 n
7 i
8
9 z
10 h
11 o
12 n
13 g
14
15 g
16 u
17 o
[1, 4, 7, 13, 17]
Process finished with exit code 0