gpt4 book ai didi

python - 图的节点数不匹配

转载 作者:可可西里 更新时间:2023-11-01 10:44:41 25 4
gpt4 key购买 nike

我有一个 MDB 数据库,其中包含有关论坛帖子的以下属性:

thread
author (posted in the thread)
children (a list of authors who replied to the post)
child_count (number of children in the list)

我正在尝试使用以下节点构建图表:

thread
author
child authors

我的数据库中的不同作者总数超过 30,000,但生成的作者计数图表大约为 3000。或者,在总共 33000 个节点中,以下代码生成大约 5000。这里发生了什么?

for doc in coll.find():

thread = doc['thread'].encode('utf-8')
author_parent = doc['author'].encode('utf-8')
children = doc['children']
children_count = len(children)
#print G.nodes()

#print post_parent, author, doc['thread']
try:
if thread in G:
continue
else:
G.add_node(thread, color='red')
thread_count+=1


if author_parent in G:
G.add_edge(author_parent, thread)
else:
G.add_node(author_parent, color='green')
G.add_edge(author_parent, thread, weight=0)
author_count+=1


if doc['child_count']!=0:
for doc in children:
if doc['author'].encode("utf-8") in G:
print doc['author'].encode("utf-8"), 'in G'
G.add_edge(doc['author'].encode("utf-8"), author_parent)
else:
G.add_node(doc['author'].encode("utf-8"),color='green')
G.add_edge(doc['author'].encode("utf-8"), author_parent, weight=0)
author_count+=1

except:
print "failed"
nx.write_dot(G,PATH)

print thread_count, author_count, children_count

最佳答案

我得到了答案。 continue 语句跳到下一次迭代,所以我以这种方式丢失了很多节点。

关于python - 图的节点数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com