我一直在嘗試向我正在創建的此節點添加名稱等屬性,但收到錯誤消息。 錯誤 # add name as a property to each node # with networkX each node is a dictionary G.add_node(tweeter_id,'name' = tweeter_name) G.add_node(interact_id,'name' = interact_name)這是我得到的錯誤。 錯誤信息 File "<ipython-input-31-55b9aecd990d>", line 28 G.add_node(tweeter_id,'name' = tweeter_name) ^ SyntaxError: expression cannot contain assignment, perhaps you meant "=="?作為參考,這是整個代碼:import networkx as nx# define an empty Directed Graph# A directed graph is a graph where edges have a direction# in our case the edges goes from user that sent the tweet to# the user with whom they interacted (retweeted, mentioned or quoted)#g = nx.Graph()G = nx.DiGraph()# loop over all the tweets and add edges if the tweet include some interactionsfor tweet in tweet_list: # find all influencers in the tweet tweeter, interactions = getAllInteractions(tweet) tweeter_id, tweeter_name = tweeter tweet_id = getTweetID(tweet) # add an edge to the Graph for each influencer for interaction in interactions: interact_id, interact_name = interaction # add edges between the two user ids # this will create new nodes if the nodes are not already in the network # we also add an attribute the to edge equal to the id of the tweet G.add_edge(tweeter_id, interact_id, tweet_id=tweet_id) # add name as a property to each node # with networkX each node is a dictionary G.add_node(tweeter_id, tweeter_name) G.add_node(interact_id, interact_name)
3 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
正如您可以從文檔中讀到的
使用關鍵字設置/更改節點屬性:
>>>?G.add_node(1,size=10) >>>?G.add_node(3,weight=0.4,UTM=('13S',382871,3972649))
因此,請使用關鍵字參數name
來定義屬性。
(注意:沒有 Quotes,否則它將是 astr
并且會導致SyntaxError
您面臨的,因為它是對字符串文字的賦值)
做這樣的事情:
????#?add?name?as?a?property?to?each?node ????#?with?networkX?each?node?is?a?dictionary ????G.add_node(tweeter_id,?name?=?tweeter_name) ????G.add_node(interact_id,?name?=?interact_name)

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
發生這種情況是因為add_node
是一個函數。而且你不能給函數賦值。
當你嘗試時,你正在這樣做:
G.add_node(tweeter_id,?'name')?=?tweeter_name
我相信你想做的是:
G.add_node(tweeter_id,?attr_dict={'name':?tweeter_name})
或作為提比布人。M指出,你還可以這樣做:
G.add_node(tweeter_id,?name=tweeter_name)

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
而不是你的
G.add_node(tweeter_id,'name') = tweeter_name G.add_node(interact_id,'name') = interact_name
使用
G.add_node(tweeter_id, tweeter_name) G.add_node(interact_id, interact_name)
(假設您已將一些字符串分配給變量tweeter_name
和interact_name
)。
添加回答
舉報
0/150
提交
取消