TypeError:“>"'dict'和'dict'实例之间不受支持 [英] TypeError: '>' not supported between instances of 'dict' and 'dict'

查看:94
本文介绍了TypeError:“>"'dict'和'dict'实例之间不受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用字典,但出现以下错误

I'm working with dictionaries and I have the following error

'>' not supported between instances of 'dict' and 'dict'

我知道Python 2.7和3.x版本的字典存在一些问题.

I know that there are some problems with dictionaries in Python 2.7 and 3.x version.

print("number of nodes %d" % G.number_of_nodes())
print("number of edges %d" % G.number_of_edges())
print("Graph is connected?: %s" % nx.is_connected(G))
print("Number of connected components: %s" % nx.number_connected_components(G))
print("Size of connected componnents: %s" % [len(cc) for cc in nx.connected_components(G)])
print("Network Analysis will be performed on the largest cc from now on") 
largest_cc = max(nx.connected_component_subgraphs(G), key=len) 

dict_communities={}
num_communities=max([y for x,y in largest_cc.nodes(data=True)]).values()[0]
for i in range (1,num_communities+1):
    dict_communities[i] = [x for x,y in largest_cc.nodes(data=True) if y['community']==i]

TypeError                                 Traceback (most recent call last)
<ipython-input-12-fd6e5cb0ddb5> in <module>
      1 dict_communities={}
----> 2 num_communities=max([y for x,y in largest_cc.nodes(data=True)])[0]
      3 for i in range (1,num_communities+1):
      4     dict_communities[i] = [x for x,y in largest_cc.nodes(data=True) if y['community']==i]

TypeError: '>' not supported between instances of 'dict' and 'dict'

推荐答案

在networkx中, graph.nodes(data = True)返回一个带有节点参数的node_id-dicts元组列表.但是在Python中,无法比较字典(调用 max 函数时要尝试将它们进行比较).您应该使用另一种方法来完成此操作,例如使用如下代码提取每个节点的特定参数:

In networkx, graph.nodes(data=True) returns a list of node_id-dicts tuples with node arguments. But in Python dicts can't be compared (you are trying to compare them when you are calling max function). You should do it with some another way, like extracting the particular argument of each node with code like this:

max([y['some_argument'] for x,y in largest_cc.nodes(data=True)])
           ^
           |
Add it ----+


这里是示例:


Here is the example:

我们创建一个随机图,并在'arg'参数中填充随机数:

We create a random graph and fill 'arg' argument with random numbers:

import networkx as nx
import random

G = nx.gnp_random_graph(10,0.3,directed=True)
for node in G.nodes:
    G.nodes[node]['arg'] = random.randint(1, 10)

然后,我们尝试使用您的代码:

Then we are trying to use your code:

[G.nodes中的x,y为y(data = True)]

它返回:

[{'arg': 8},
 {'arg': 5},
 {'arg': 9},
 {'arg': 4},
 {'arg': 8},
 {'arg': 6},
 {'arg': 3},
 {'arg': 2},
 {'arg': 8},
 {'arg': 1}]

而且您无法将这些字典相互比较.

And you can't compare these dicts with each other.

但是如果您要在列表中指定"arg":

But if you will specify 'arg' in the list:

[G.nodes(data = True)中x,y的y ['arg']]

它将返回:

[8,1,5,3,10,5,7,10,1,2]

您可以选择最大的元素(但不要在行尾写 .values()[0] ,这会导致错误):

And you can pick the largest element (but don't write .values()[0] in the end of the line, it will cause an error):

max([.G.nodes中的x,y的y ['arg'](data = True)])

10

这篇关于TypeError:“&gt;"'dict'和'dict'实例之间不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆