## 通过NetworkX中的边和节点属性查询图的最佳方法？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (76)

``````G2 = nx.MultiDiGraph()
G2.add_node( "UserA", { "type" :"Cat" } )
G2.add_node( "UserB", { "type" :"Dog" } )
G2.add_node( "UserC", { "type" :"Mouse" } )
G2.add_node( "Likes", { "type" :"Feeling" } )
G2.add_node( "Hates", { "type" :"Feeling" } )

G2.add_edge( "UserA", 'Hates' ,  statementid="1" )
G2.add_edge( "Hates", 'UserB' ,  statementid="1"  )
G2.add_edge( "UserC", 'Hates' ,  statementid="2" )
G2.add_edge( "Hates", 'UserA' ,  statementid="2"  )
G2.add_edge( "UserB", 'Hates' ,  statementid="3"  )
G2.add_edge( "Hates", 'UserA' ,  statementid="3"  )
G2.add_edge( "UserC", 'Likes' ,  statementid="3"  )
G2.add_edge( "Likes", 'UserB' ,  statementid="3"  )
``````

`如下`

``````for node,data in G2.nodes_iter(data=True):
if ( data['type'] == "Cat" ):
# get all edges out from these nodes
#then recursively follow using a filter for a specific statement_id

#or get all edges with a specific statement id
# look for  with a node attribute of "cat"
``````

```import networkx as nx

G = nx.Graph()

# method 1
fish = (n for n in G if G.node[n]['label']=='fish')
# method 2
fish2 = (n for n,d in G.nodes(data=True) if d['label']=='fish')

print(list(fish))
print(list(fish2))

red = ((u,v) for u,v,d in G.edges(data=True) if d['color']=='red')

print(list(red))```

```labels = {}
for n, d in G.nodes(data=True):
l = d['label']
labels[l] = labels.get(l, [])
labels[l].append(n)
print labels```

### 腾讯云 COS 怎么才能外链调用 m3u8 到别的网站播放？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### Ubuntu搭建的WordPress如何修改php.ini？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

php新手很多不知道怎么查配置文件在哪，这里提供一个很简单的方法 使用 php -i 命令可以打印php的详细信息，可以把这堆东西输出一下 php -i > outputphp.txt，结合 grep 查找命令 php -i| grep php.ini 打印结果如下 Config...... 展开详请

### 归档存储采用的存储介质是什么, 安全可靠吗？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### redis数据库应该怎样连接？？？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### 如何使用holer实现从外网访问本地WEB应用？

Dingda

Dingda · 站长 (已认证)