Python docs tutorial の Looping Techniques Chapter を読んだところですが、この少年について質問があります:[:]
文字列の開始インデックスと終了インデックスを取ることがわかったので、次のようにします。
text = "This is text"
text[:] # will return the whole text "This is text" and
tex[:4] # will return "This".
しかし、ここでこのコードを見たとき...
words = ['cat', 'dog', 'cowcowcow']
for w in words[:]: # Loop over a slice copy of the entire list.
if len(w) > 6:
words.insert(0, w)
print words
出力:
['cowcowcow', 'cat', 'dog', 'cowcowcow']
[:]
... for ループの意味がわかりませんでした。私はただ書くだろう
for w in words:
しかし、そうすると、無限のwhileループになります。なぜですか?