Skip to content Skip to sidebar Skip to footer

Typeerror: '>' Not Supported Between Instances Of 'dict' And 'dict'

I'm working with dictionaries and I have the following error '>' not supported between instances of 'dict' and 'dict' I know that there are some problems with dictionaries in P

Solution 1:

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:

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:

[y for x,y in G.nodes(data=True)]

It returns:

[{'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.

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

[y['arg'] for x,y in G.nodes(data=True)]

It will return:

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

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([y['arg'] for x,y in G.nodes(data=True)])

10

Post a Comment for "Typeerror: '>' Not Supported Between Instances Of 'dict' And 'dict'"