Coding/Python Matlab

파이썬 - enumerate

smores 2012. 12. 18. 02:50

>>> mylist=['a','b','c','d']

>>> for i,j in enumerate(mylist):

...     print i,j

...

0 a

1 b

2 c

3 d

>>> for j,i in enumerate(mylist):

...     print i,j

...

a 0

b 1

c 2

d 3

>>> for j,i in enumerate(mylist,3):

...     print i,j

...

a 3

b 4

c 5

d 6

>>> for j,i in enumerate(3,mylist):

...     print i,j

...

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'list' object cannot be interpreted as an index

>>>



추가로 읽을만한 내용