亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何給節點添加屬性?

如何給節點添加屬性?

郎朗坤 2023-10-06 16:39:19
我一直在嘗試向我正在創建的此節點添加名稱等屬性,但收到錯誤消息。 錯誤  # 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)


查看完整回答
反對 回復 2023-10-06
?
呼喚遠方

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)


查看完整回答
反對 回復 2023-10-06
?
繁華開滿天機

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_nameinteract_name)。


查看完整回答
反對 回復 2023-10-06
  • 3 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號