HackerRank

Really fun regex one.

import re

n = int(input())
txt = ''
for _ in range(n):
    str = input()
    txt = txt + str

dict = {}
all = re.findall('<[^/][^<>]*>', txt)
for i in range(len(all)):
    segs = re.split(' ', all[i][1:-1])    
    if not segs[0] in dict:
        dict[segs[0]] = set([])
    if (len(segs) > 1):
        tags = re.findall("s[a-z]+=", all[i][1:-1])
        for j in range(len(tags)):        
            dict[segs[0]].add(tags[j][1:-1])        

sortedKeys = list(dict.keys())
sortedKeys.sort()
for k in sortedKeys:
    print(k + ':', end="")
    tags = list(dict[k])
    tags.sort()
    print (','.join(tags))
原文地址:https://www.cnblogs.com/tonix/p/4548181.html