簡體   English   中英

用Python打印元組索引

[英]Print Tuple Index in Python

這個問題屬於“是-可行,是-這很丑,是-可能有更好的方法”類別。 我想使用正則表達式將組拉出匹配項,然后打印組號和組值。 這是為了向某人展示正則表達式如何工作並跟蹤每個組的值。 起作用的代碼是:

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)

有沒有更好的方法來打印每個元組條目的索引?

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

(並且變量不應以大寫字母開頭...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM