计数

转自:http://safehammad.com/downloads/python-idioms-2014-01-16.pdf

# GOOD

names = ['Safe', 'George', 'Mildred']

for i, name in enumerate(names):

  print(i, name) # 0 Safe, 1 George etc.

# NOT SO GOOD

names = ['Safe', 'George', 'Mildred']

count = 0

for name in names:

  print(name) # 0 Safe, 1 George etc.

  count += 1

原文地址:https://www.cnblogs.com/wzjbg/p/7425134.html