Count Boxer
Count Boxer

Reputation: 703

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?

Upvotes: 0

Views: 1993

Answers (1)

kennytm
kennytm

Reputation: 523224

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

(and the variables should not start with a capital letter...)

Upvotes: 3

Related Questions