Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
38049bb2
Kaydet (Commit)
38049bb2
authored
Eki 16, 2012
tarafından
Chris Jerdonek
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Issue #16225: Merge from 3.2: Add additional note to tutorial about looping.
üst
39b86700
4fab8f0e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
11 deletions
+24
-11
controlflow.rst
Doc/tutorial/controlflow.rst
+11
-11
datastructures.rst
Doc/tutorial/datastructures.rst
+13
-0
No files found.
Doc/tutorial/controlflow.rst
Dosyayı görüntüle @
38049bb2
...
@@ -58,24 +58,24 @@ they appear in the sequence. For example (no pun intended):
...
@@ -58,24 +58,24 @@ they appear in the sequence. For example (no pun intended):
::
::
>>> # Measure some strings:
>>> # Measure some strings:
...
a
= ['cat', 'window', 'defenestrate']
...
words
= ['cat', 'window', 'defenestrate']
>>> for
x in a
:
>>> for
w in words
:
... print(
x, len(x
))
... print(
w, len(w
))
...
...
cat 3
cat 3
window 6
window 6
defenestrate 12
defenestrate 12
It is not safe to modify the sequence being iterated over in the loop (this can
If you need to modify the sequence you are iterating over while inside the loop
only happen for mutable sequence types, such as lists). If you need to modify
(for example to duplicate selected items), it is recommended that you first
the list you are iterating over (for example, to duplicate selected items) you
make a copy. Iterating over a sequence does not implicitly make a copy. The
must iterate over a copy. The slice notation makes this particularly
slice notation makes this especially convenient::
convenient::
>>> for x in a[:]: # make a slice copy of the entire list
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(x) > 6: a.insert(0, x)
... if len(w) > 6:
... words.insert(0, w)
...
...
>>>
a
>>>
words
['defenestrate', 'cat', 'window', 'defenestrate']
['defenestrate', 'cat', 'window', 'defenestrate']
...
...
Doc/tutorial/datastructures.rst
Dosyayı görüntüle @
38049bb2
...
@@ -589,6 +589,19 @@ returns a new sorted list while leaving the source unaltered. ::
...
@@ -589,6 +589,19 @@ returns a new sorted list while leaving the source unaltered. ::
orange
orange
pear
pear
To change a sequence you are iterating over while inside the loop (for
example to duplicate certain items), it is recommended that you first make
a copy. Looping over a sequence does not implicitly make a copy. The slice
notation makes this especially convenient::
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
.. _tut-conditions:
.. _tut-conditions:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment