开发者

Extracting and integrating data from separate lists in Python

开发者 https://www.devze.com 2022-12-31 19:51 出处:网络
I have this code: cursor.execute( \'\'\' SELECT id,DISTINCT tag FROM userurltag \'\'\') tags = cursor.fetchall ()

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" l开发者_开发百科ist 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号