Aggregate sets according to keys with defaultdict python

前端 未结 1 524
猫巷女王i
猫巷女王i 2021-01-29 04:59

I have a bunch of lines in text with names and teams in this format:

Team (year)|Surname1, Name1

e.g.

Yankees (1993)|Abbot, Jim         


        
相关标签:
1条回答
  • 2021-01-29 05:38

    You can use a tuple as a key here, for eg. ('Yankees', '1994'):

    from collections import defaultdict
    dic = defaultdict(list)
    with open('abc') as f:
        for line in f:
            key,val  = line.split('|')
            keys = tuple(x.strip('()') for x in key.split())
            vals = [x.strip() for x in val.split(', ')]
            dic[keys].append(vals)
    print dic
    for k,v in dic.iteritems():
        print "{}({})|{}".format(k[0],k[1],"|".join([", ".join(x) for x in v]))
    

    Output:

    defaultdict(<type 'list'>, 
    {('Yankees', '1994'): [['Abbot', 'Jim']],
     ('Yankees', '2000'): [['Buddies', 'Mike'], ['Canseco', 'Jose']],
     ('Yankees', '1993'): [['Abbot', 'Jim'], ['Assenmacher', 'Paul']]})
    
    Yankees(1994)|Abbot, Jim
    Yankees(2000)|Buddies, Mike|Canseco, Jose
    Yankees(1993)|Abbot, Jim|Assenmacher, Paul
    
    0 讨论(0)
提交回复
热议问题