Python相当于Ruby的each_with_index?

ASh*_*lly 12 ruby python equivalent

在Ruby中,如果我有一个数组并且我想在循环中使用索引和值,我会使用each_with_index.

a=['a','b','c']
a.each_with_index{|v,i| puts("#{i} : #{v}") } 
Run Code Online (Sandbox Code Playgroud)

版画

0 : a 
1 : b
2 : c
Run Code Online (Sandbox Code Playgroud)

什么是Pythonic做同样的事情?

kla*_*ske 7

就像是:

for i, v in enumerate(a):
   print "{} : {}".format(i, v)
Run Code Online (Sandbox Code Playgroud)