Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;“关键错误”;尝试访问networkx中的节点属性时_Python_Twitter_Networkx_Graph Theory - Fatal编程技术网

Python &引用;“关键错误”;尝试访问networkx中的节点属性时

Python &引用;“关键错误”;尝试访问networkx中的节点属性时,python,twitter,networkx,graph-theory,Python,Twitter,Networkx,Graph Theory,我有一个推特追随者网络。每个follower都有多个属性,比如follower count、username、location等。我正在尝试查找图中所有前置节点的follower计数。我可以使用networokx中的preference函数来执行此操作,但当我尝试访问节点属性时,跟随者计数会出现“关键错误”。 这是我的密码 if len(list(DG.predecessors(node))) > 0: #only loop through nodes that have 1 or

我有一个推特追随者网络。每个follower都有多个属性,比如follower count、username、location等。我正在尝试查找图中所有前置节点的follower计数。我可以使用networokx中的
preference
函数来执行此操作,但当我尝试访问节点属性时,跟随者计数会出现“关键错误”。 这是我的密码

   if len(list(DG.predecessors(node))) > 0: #only loop through nodes that have 1 or more predecsoor
       for pred_node in DG.predecessors(node):
       a = DG.nodes[pred_node]['follower_count'] # Key value error here

当我尝试将其分配给

这是否可以归结为我如何设置属性?有关如何设置属性的信息,请参见下面的代码



import csv
import networkx as nx


with open('twitter_nodelist.csv', 'r') as nodecsv: # Open the file
    nodereader = csv.reader(nodecsv) # Read the csv
   
    nodes = [n for n in nodereader][1:]

node_names = [n[0] for n in nodes] # Get a list of only the node names

with open('twitter_edgelist.csv', 'r') as edgecsv: # Open the file
    edgereader = csv.reader(edgecsv) # Read the csv
    edges = [tuple(e) for e in edgereader][1:] # Retrieve the data
    
print(len(node_names))

print(len(edges))



DG = nx.Graph()

DG.add_nodes_from(node_names)
DG.add_edges_from(edges)

total_nodes = DG.number_of_nodes()

print(nx.info(DG))

print(total_nodes)


username_dict = {}
user_screen_name_dict = {}
tweetid_dict = {}
tweet_text_dict = {}
user_followers_count_dict = {}
user_friends_count_dict = {}
user_location_dict = {}
user_url_dict = {}
tweet_created_at_dict = {}
tweet_source_dict = {}




for node in nodes: # Loop through the list, one row at a time
    username_dict[node[0]] = node[1]
    user_screen_name_dict[node[0]] = node[2]
    tweet_text_dict[node[0]] = node[3]
    user_followers_count_dict[node[0]] = node[4]
    user_friends_count_dict[node[0]] = node[5]
    user_location_dict = node[6]
    user_url_dict = node[7]
    tweet_created_at_dict = node[8]
    tweet_source_dict = node[9]



nx.set_node_attributes(G, username_dict, 'username')
nx.set_node_attributes(G, user_screen_name_dict, 'screen_name')
nx.set_node_attributes(G, tweet_text_dict, 'tweet_text')
nx.set_node_attributes(G, user_followers_count_dict, 'follower_count')
nx.set_node_attributes(G, user_friends_count_dict, 'friend_count')
nx.set_node_attributes(G, user_location_dict, 'location')
nx.set_node_attributes(G, user_url_dict, 'URL')
nx.set_node_attributes(G, tweet_created_at_dict, 'tweet_creation_time')
nx.set_node_attributes(G, tweet_source_dict, 'tweet_source')
我试过打印字典,但它们打印时没有问题


提前谢谢

把它弄明白了。结果是我没有正确设置节点属性。我用G而不是DG