Skip to content

Instantly share code, notes, and snippets.

@workze
Created April 18, 2020 08:29
Show Gist options
  • Save workze/ebd7eda1661159ebb05722c4f5f433c7 to your computer and use it in GitHub Desktop.
Save workze/ebd7eda1661159ebb05722c4f5f433c7 to your computer and use it in GitHub Desktop.
python list
list = ['html', 'js', 'css', 'python']
list = ['html', 'js', 'css', 'python']
# 方法1
print '遍历列表方法1:'
for i in list:
print ("序号:%s 值:%s" % (list.index(i) + 1, i))
print '\n遍历列表方法2:'
# 方法2
for i in range(len(list)):
print ("序号:%s 值:%s" % (i + 1, list[i]))
# 方法3
print '\n遍历列表方法3:'
for i, val in enumerate(list):
print ("序号:%s 值:%s" % (i + 1, val))
# 方法3
print '\n遍历列表方法3 (设置遍历开始初始位置,只改变了起始序号):'
for i, val in enumerate(list, 2):
print ("序号:%s 值:%s" % (i + 1, val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment