提取和使用Python
问题描述:
整合来自不同列表中的数据我有这样的代码:提取和使用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
心连心
读了三遍......我真的不明白。 – 2010-05-21 16:34:30
提供最少的示例(给出'tags'列出您希望看到的输出)。 – jfs 2010-05-21 16:53:56