如何使用enumerate帮助查找包含'x'的所有单词的索引
谢谢
wordsFile = open("words.txt", 'r')
words = wordsFile.read()
wordsFile.close()
wordList = words.split()
indices=[]
for (index, value) in enumerate(wordList):
if value == 'x':
print("These locations contain words containing the letter 'x':n",indices)
你的代码差不多完成了:
for (index, value) in enumerate(wordList):
if 'x' in value:
indices.append(index)
检查每个单词中是否有x
。如果是,则将索引添加到indices
。