for x in range(10):
然后为了获得计数,它只是x.但是说我有一个使用列表的for循环:
layer = [somedata,someotherdata...etc]for row in layer: print #the number the loop is on
有没有办法做到这一点,除了指定一个整数计数变量并逐渐递增它,像这样?
layer = [somedata]count = 0for row in layer: print count count += 1解决方法 您可以使用枚举.这将为您提供每次迭代的计数和您正在迭代的值.
注意:像范围一样,您可以指定开始计数的索引.
for count,row in enumerate(layer): print count总结
以上是内存溢出为你收集整理的python – 有没有办法让for循环的运行计数不使用范围?全部内容,希望文章能够帮你解决python – 有没有办法让for循环的运行计数不使用范围?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)