获取字符串‘is’在列表word中下标
第一种
word=[‘dislike’, ‘is’, ‘life’, ‘like’, ‘long’, ‘python’, ‘short’, ‘too’,‘is’]
word1 = [i for i in enumerate(word)]# 或者用print(list(enumerate(list1)))
print(word1)
list_num1=[]
for i in word1:
if i[1] == 'is':
list_num1.append(i[0])
print(list_num1)
第二种
解释下[i for i, x in enumerate(word) if x==‘is’]的意思是:取得’is’在列表word的下标,并用列表表示出来。
word=[‘dislike’, ‘is’, ‘life’, ‘like’, ‘long’, ‘python’, ‘short’, ‘too’,‘is’]
word3=[i for i, x in enumerate(word) if x=='is']
print(word3)
第三种
word=[‘dislike’, ‘is’, ‘life’, ‘like’, ‘long’, ‘python’, ‘short’, ‘too’,‘is’]
list_num2=[]
j = 0
while j <=len(word)-1:
if word1[j][1] == 'is':
list_num2.append(word1[j][0])
j += 1
print(list_num2)
以上的结果是:
补充: