Python 相当于 Ruby 的 each_with_index?

标签 python ruby equivalent

<分区>

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

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

打印

0 : a 
1 : b
2 : c

做同样事情的 Pythonic 方式是什么?

最佳答案

类似于:

for i, v in enumerate(a):
   print "{} : {}".format(i, v)

关于Python 相当于 Ruby 的 each_with_index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569577/

相关文章:

ruby - 如何测试字符串是否包含 ruby​​ 中的两个或多个元音?

postgresql - PostgreSQL 中的 DATEADD 等价物

python - 用 numpy 和/或 scipy 插入 3D 体积

Python 字典类, 'KeyError'

ruby-on-rails - 将 Square 与 Rails 项目集成

ruby - 从 ruby​​ 脚本更改调用 shell 的当前工作目录

delphi - Delphi 中等效的运算符

c# - Java 的 "for (String currLine: allLines)"的 C# 等价物是什么?

python - BeautifulSoup 寻找 xml 标签

python - 如何组合两个具有不同行大小的数据框?