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
f0454556
Kaydet (Commit)
f0454556
authored
Eki 21, 2009
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Update advice on how to implement a queue.
üst
3f902a0e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
9 deletions
+13
-9
datastructures.rst
Doc/tutorial/datastructures.rst
+13
-9
No files found.
Doc/tutorial/datastructures.rst
Dosyayı görüntüle @
f0454556
...
...
@@ -138,21 +138,25 @@ Using Lists as Queues
.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
It is also possible to use a list as a queue, where the first element added is
the first element retrieved ("first-in, first-out"); however, lists are not
efficient for this purpose. While appends and pops from the end of list are
fast, doing inserts or pops from the beginning of a list is slow (because all
of the other elements have to be shifted by one).
You can also use a list conveniently as a queue, where the first element added
is the first element retrieved ("first-in, first-out"). To add an item to the
back of the queue, use :meth:`append`. To retrieve an item from the front of
the queue, use :meth:`pop` with ``0`` as the index. For example::
To implement a queue, use :class:`collections.deque` which was designed to
have fast appends and pops from both ends. For example::
>>> queue = ["Eric", "John", "Michael"]
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.pop
(0)
>>> queue.pop
left() # The first to arrive now leaves
'Eric'
>>> queue.pop
(0)
>>> queue.pop
left() # The second to arrive now leaves
'John'
>>> queue
['Michael', 'Terry', 'Graham']
>>> queue
# Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
.. _tut-functional:
...
...
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