简体   繁体   中英

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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