繁体   English   中英

从Python的单独列表中提取和集成数据

[英]Extracting and integrating data from separate lists in Python

我有以下代码:

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] 

我有7个群组名称1,...,7。 “标签”列表中的每一行对应于“ T”列表中的一行。“ T”的值表示例如“标签”列表中的第一行属于组3,“标签”列表中的第二行属于第5组,依此类推。 这些基本上是每个标签所属的群集。 我要提取它们,以使每个组/集群具有单独的例如字典数据类型。 重要的是,每次运行时簇的数量都会改变。 因此,我需要一个通用的代码来解决这个问题,并且可以使用各种数量的群集。 我非常需要您的帮助谢谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM