When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.
To loop over two or more sequences at the same time, the entries can be paired with thezip() function.
To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.
To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.
When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the iteritems() method.
It is sometimes tempting to change a list while you are looping over it; however, it is often simpler and safer to create a new list instead.
출처 : https://docs.python.org/2/tutorial/datastructures.html#tut-loopidioms
'List > Python' 카테고리의 다른 글
httplib source_address example (0) | 2016.11.12 |
---|---|
Ruby 설치 (Windows, Ubuntu) (0) | 2015.10.02 |
Quick Sort (0) | 2015.06.18 |
import random (0) | 2015.06.17 |
lsm.py (0) | 2015.06.09 |