关于for和enumerate的问题,欢迎指导
且看下面一段代码#!/user/bin/python
#-*-coding:UTF-8-*-
# Filename: enumerate.py
values=[12,13,14,15,16,78,88.0]
a=[]
b=[]
c=[]
d=[]
e=[]
for (i,v) in enumerate(values):
a.append([i,v])
e.append([i,v])
print a
print e
for (i,v) in enumerate(values):
b.append([i,v])
print b
for (i,v) in enumerate(values):
c.append([i,v])
print c
for (i,v) in enumerate(values):
d.append([i,v])
print d
再看下面一段为什么结果不同?
!/user/bin/python
#-*-coding:UTF-8-*-
# Filename: enumerate.py
values=[12,13,14,15,16,78,88.0]
vs=enumerate(values)
a=[]
b=[]
c=[]
d=[]
e=[]
for (i,v) in vs:
a.append([i,v])
e.append([i,v])
print a
print e
#vs=enumerate(values)
for (i,v) in vs:
b.append([i,v])
print b
for (i,v) in vs:
c.append([i,v])
print c
for (i,v) in vs:
d.append([i,v])
print d