作者angleevil (朦胧寻光)
看板Python
标题[问题] 关於python for 回圈的疑问
时间Thu Apr 6 11:03:35 2017
hello 大家好,最近在自学python.
不知道是版本的问题还是怎样.
一直搞不懂python for的逻辑.
简单的问题如下:
#! /usr/bin/python
my_list=[1,2,3,1,3]
for ii in my_list:
print my_list[ii]
列印出来却是
2
3
1
2
1
这样我根本搞不懂for的逻辑阿, ii是从0开始算起嘛?? list的index是从0开始嘛??
ps:我的版本是python 2.7, 有点乱印的感觉.
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.124.107.139
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1491447820.A.044.html
1F:推 oeg: print ii 就好 04/06 11:06
2F:→ zerof: ii 是 1,2,3,1,3 ,转 index 进 my_list 就会变 2,3,1,2,1 04/06 11:09
3F:→ angleevil: 阿...我懂了. 被c的观念搞混了 04/06 11:09
4F:推 coeric: 改成for ii in range(len(my_list): 不过这比较累 04/06 11:17
5F:→ coeric: 同一楼即可 04/06 11:18
6F:推 ckc1ark: 还可以试试for i, num in enumerate(mylist): 04/06 11:30
7F:推 shimachokong: list的index是0开始没错 04/06 11:38
8F:推 Yshuan: 看一下iterator的观念吧 04/06 16:02
9F:推 simpleplanya: 推 04/06 18:21
10F:→ simpleplanya: 一楼 04/06 18:21
11F:→ brightwish: python的for 是C的foreach 04/06 19:27
12F:推 grapherd: 无关话题,建议使用3.6,够稳定了 04/06 19:30
13F:推 minikai: 你print的不是list的值啊,是list index後的值... 04/08 17:41