如何为 NetworkX 2.0 更新这个?
How to update this for NetworkX 2.0?
我正在尝试写这篇文章,但我刚刚意识到 attr_dict
在新版本的 NetworkX 中不受支持,因为我没有从下面的代码中获得所需的行。
有人能告诉我如何更新这段代码吗?添加行部分是什么问题,因为不再支持 attr_dict
。
if not this_user_id in G:
G.add_node(this_user_id, attr_dict={
'followers': row['followers'],
'age': row['age'],
})
这是上下文中的代码
# Gather the data out of the row
#
this_user_id = row['author']
author = row['retweet_of']
followers = row['followers']
age = row['age']
rtfollowers = row['rtfollowers']
rtage = row['rtage']
#
# Is the sender of this tweet in our network?
#
if not this_user_id in G:
G.add_node(this_user_id, attr_dict={
'followers': row['followers'],
'age': row['age'],
})
#
# If this is a retweet, is the original author a node?
#
if author != "" and not author in G:
G.add_node(author, attr_dict={
'followers': row['rtfollowers'],
'age': row['rtage'],
})
#
# If this is a retweet, add an edge between the two nodes.
#
if author != "":
if G.has_edge(author, this_user_id):
G[author][this_user_id]['weight'] += 1
else:
G.add_weighted_edges_from([(author, this_user_id, 1.0)])
nx.write_gexf(G, 'tweets1.gexf')
现在 add_node
函数直接接受节点属性作为关键字参数,因此您可以将函数重新表述为:
G.add_node(this_user_id, followers = row['followers'],age = row['age'])
或者如果您将属性保存在名为 my_attr_dict
的字典中,您可以说
G.add_node(this_user_id,**my_attr_dict)
我正在尝试写这篇文章,但我刚刚意识到 attr_dict
在新版本的 NetworkX 中不受支持,因为我没有从下面的代码中获得所需的行。
有人能告诉我如何更新这段代码吗?添加行部分是什么问题,因为不再支持 attr_dict
。
if not this_user_id in G:
G.add_node(this_user_id, attr_dict={
'followers': row['followers'],
'age': row['age'],
})
这是上下文中的代码
# Gather the data out of the row
#
this_user_id = row['author']
author = row['retweet_of']
followers = row['followers']
age = row['age']
rtfollowers = row['rtfollowers']
rtage = row['rtage']
#
# Is the sender of this tweet in our network?
#
if not this_user_id in G:
G.add_node(this_user_id, attr_dict={
'followers': row['followers'],
'age': row['age'],
})
#
# If this is a retweet, is the original author a node?
#
if author != "" and not author in G:
G.add_node(author, attr_dict={
'followers': row['rtfollowers'],
'age': row['rtage'],
})
#
# If this is a retweet, add an edge between the two nodes.
#
if author != "":
if G.has_edge(author, this_user_id):
G[author][this_user_id]['weight'] += 1
else:
G.add_weighted_edges_from([(author, this_user_id, 1.0)])
nx.write_gexf(G, 'tweets1.gexf')
现在 add_node
函数直接接受节点属性作为关键字参数,因此您可以将函数重新表述为:
G.add_node(this_user_id, followers = row['followers'],age = row['age'])
或者如果您将属性保存在名为 my_attr_dict
的字典中,您可以说
G.add_node(this_user_id,**my_attr_dict)