【python 字母索引】找到英文句子裡面每個單詞最後一個的索引
阿新 • • 發佈:2018-12-18
# -*- coding:utf-8 -*- sentence="shi yuan li mei" 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 s
1 h
2 i
3
4 y
5 u
6 a
7 n
8
9 l
10 i
11
12 m
13 e
14 i
[2, 7, 10, 14]
Process finished with exit code 0