![](/img/trans.png)
[英]How do I use the enumerate function for CodeHS 8.4.13: Owls, Part 2?
[英]How do I complete the 8.4.13 Owls, Part 2 problem in CodeHS Python?
所以作業說:
除了只報告包含單詞 owl 的單詞數量之外,您還應該報告單詞出現的索引! 以下是您的程序運行示例,可能如下所示:
Enter some text: Owls are so cool! I think snowy owls might be my faborite. Or maybe spotted owls.
There were 3 words that contained "owl".
They occurred at indices: [0, 7, 15]
到目前為止,這是我的代碼:
sentence = input("Talk about something: ")
sentence.lower()
print "There are " + str(sentence.count("owls)) + " of the word \"owls\" in the sentence"
print "They occurred at indices: " + str(sentence.findl("owls")
但這僅打印它找到的第一個索引,我如何讓它打印它找到的所有索引?
sentence = 'Owls are so cool! I think snowy owls might be my faborite. Or maybe spotted owls.'
words = [i for i, word in enumerate(sentence.split(' ')) if 'owl' in word.lower()]
這有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.