List and order

N

Nic

Hello,
I'm using the NetworkX Python package (https://networkx.lanl.gov/).
Through this package I wrote the following code:

import networkx as NX
G=NX.Graph()
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
ddeg=G.degree(with_labels=True)
for (u,v) in G.edges():
print ddeg,ddeg[v]

As result, I have:

12
22
21

I'd like to order the numbers included in the couples in a decrescent way:

12
12
22

And then to write the couples on a single line, always in a decrescent way:
12 12 22

I'm not able to do this operation. Can you help me please?
Thanks a bunch,
Nic
 
M

Miki

Hello Nic,

Python lists has a very powerfull buid-in "sort".

edges = list(G.edges())
edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
for u, v in edges:
print u,v, # Note the training comma to avoid newline
print

HTH,
Miki
http://pythonwise.blogspot.com
 
N

Nic

Hello Miki,

Many thanks for the support.
I tried to insert and execute the code, but the following error happens:

Traceback (most recent call last):
File "grafodna.py", line 10, in ?
edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
TypeError: <lambda>() takes exactly 2 arguments (1 given)

Do you know how is it possible to delete it?
Thanks.

Nic
 
P

Peter Otten

Nic said:
I tried to insert and execute the code, but the following error happens:

Traceback (most recent call last):
File "grafodna.py", line 10, in ?
edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
TypeError: <lambda>() takes exactly 2 arguments (1 given)

Do you know how is it possible to delete it?

Note that

lambda a, b: ...

takes two arguments while

lambda (a, b): ...

takes one argument which must be a sequence (list, string, generator,...) of
two items.

So the above should probably be

edges = list(G.edges())
edges.sort(key=lambda (u, v): (ddeg, ddeg[v]))
for u, v in edges:
print ddeg, ddeg[v],
print


Here's how I would do it:

edges = [(ddeg, ddeg[v]) for u, v in G.edges()]
edges.sort()
for a, b in edges:
print a, b,
print

(all untested)

Peter
 
N

Nic

Many thanks.
Both the cases are OK.
The only problem is that from:
12
22
21
In spite of writing
12 12 22
it writes
12 21 22
Do you know how is it possible to delete also this last trouble?
Thanks a bunch,
Nic

Peter Otten said:
Nic said:
I tried to insert and execute the code, but the following error happens:

Traceback (most recent call last):
File "grafodna.py", line 10, in ?
edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
TypeError: <lambda>() takes exactly 2 arguments (1 given)

Do you know how is it possible to delete it?

Note that

lambda a, b: ...

takes two arguments while

lambda (a, b): ...

takes one argument which must be a sequence (list, string, generator,...)
of
two items.

So the above should probably be

edges = list(G.edges())
edges.sort(key=lambda (u, v): (ddeg, ddeg[v]))
for u, v in edges:
print ddeg, ddeg[v],
print


Here's how I would do it:

edges = [(ddeg, ddeg[v]) for u, v in G.edges()]
edges.sort()
for a, b in edges:
print a, b,
print

(all untested)

Peter
 
P

Peter Otten

Nic said:
The only problem is that from:
12
22
21
In spite of writing
12 12 22
it writes
12 21 22
Do you know how is it possible to delete also this last trouble?

I thought that the two 12 were a typo, but it seems you want to reorder the
nodes inside an edge, too. Here's a fix that normalizes the edge before
sorting the list of edges:

edges = [sorted((ddeg, ddeg[v])) for u, v in G.edges()]
edges.sort()
for a, b in edges:
    print a, b,
print

Peter
 
N

Nic

Perfect. Thanks.
Nic

Peter Otten said:
Nic said:
The only problem is that from:
12
22
21
In spite of writing
12 12 22
it writes
12 21 22
Do you know how is it possible to delete also this last trouble?

I thought that the two 12 were a typo, but it seems you want to reorder
the
nodes inside an edge, too. Here's a fix that normalizes the edge before
sorting the list of edges:

edges = [sorted((ddeg, ddeg[v])) for u, v in G.edges()]
edges.sort()
for a, b in edges:
print a, b,
print

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top