views:

36

answers:

1

Hi, I have this code:

cursor.execute( ''' SELECT id,DISTINCT tag
                     FROM userurltag ''')
tags = cursor.fetchall ()
T = [3,5,7,2,1,2,2,2,5,6,3,3,1,7,4] 

I have 7 groups names 1,...,7 . Each row in "tags" list corresponds to a row in "T" list.the values of "T" say that for example the first row in "tags" list belongs to group 3, the second row in "tags" list belong to group 5 and so on. These are basically the clusters to which each tag belongs. I want to extract them, in a way that I have each group/cluster in a separate for example dictionary data type. The important thing is that the number of clusters will change in each run. So I need a general code can work with various numbers of clusters for this problem. I seriously need you help Thanks.

+1  A: 
cluster_to_tag = defaultdict(list)
#May want to assert that length of tags and T is same
for tag,cluster in zip(tags, T):
    cluster_to_tag[cluster].append(tag)

#cluster_to_tag now maps cluster ti list of tags

hth

Sijin