简体   繁体   English

用Python打印元组索引

[英]Print Tuple Index in Python

This question falls into the "yes - this works, yes - this is ugly, yes - there is probably a better way" category. 这个问题属于“是-可行,是-这很丑,是-可能有更好的方法”类别。 I want to use a regular expression to pull groups out of a match and then print the group number and the group value. 我想使用正则表达式将组拉出匹配项,然后打印组号和组值。 It is to show someone how regular expressions work and to keep track of the values of each group. 这是为了向某人展示正则表达式如何工作并跟踪每个组的值。 The code that works is: 起作用的代码是:

import re

FundTypeGroups = re.match("([A-Z]0)(\d)([A-Z])","G02A").groups()
print FundTypeGroups

for FundTypeGroup in FundTypeGroups:
    print "%s: %s" % (FundTypeGroups.index(FundTypeGroup), FundTypeGroup)

Is there a better way to print the index of each tuple entry? 有没有更好的方法来打印每个元组条目的索引?

 for index, group in enumerate(FundTypeGroups):
     print "%s: %s" % (index, group)

(and the variables should not start with a capital letter...) (并且变量不应以大写字母开头...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM