【发布时间】:2017-01-20 10:16:35
【问题描述】:
我正在尝试查找如何在 Python 列表中打印单词的索引。如果句子是“Hello world world hello name”)我希望它打印列表“1、2、2、1、3”)
我删除了所有重复的单词:
sentence = input("Enter").lower()
words = sentence.split()
counts = []
for word in words:
if word not in counts:
counts.append(word)
print(counts)
但我仍然需要使用数组获取句子的索引
【问题讨论】:
-
你有没有尝试过?
-
所以您希望列表是单词第一次出现的索引列表?请看How to Ask
-
@Chris_Rands:我认为 OP 表示序列中的第三个唯一词。
标签: python