[python]python 遍历一个list 的小例子:

时间:2023-03-09 03:26:32
[python]python 遍历一个list 的小例子:

[python]python 遍历一个list 的小例子:

mlist=["aaa","bbb","ccc"]
for ss in enumerate(mlist):
print ss

验证一下运行结果:

In [34]: mlist=["aaa","bbb","ccc"]

In [35]: for ss in enumerate(mlist):
....: print ss
....:
(0, 'aaa')
(1, 'bbb')
(2, 'ccc')

In [36]:

这个更好:

参考:
http://www.tutorialspoint.com/python/python_for_loop.htm

In [39]: fruits = ['banana','apple','mango']

In [40]: for mfruit in fruits:
....: print 'Current fruit:',mfruit
....:
Current fruit: banana
Current fruit: apple
Current fruit: mango

若是通过函数实现,可以改为这样:

In [41]: def func001 (myrow):
....: for elem in myrow:
....: print elem
....:

In [42]: func001(fruits)
banana
apple
mango

In [43]: