473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

List and order

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[u],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
May 15 '06 #1
6 1351
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

May 15 '06 #2
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

"Miki" <mi*********@gmail.com> ha scritto nel messaggio
news:11**********************@u72g2000cwu.googlegr oups.com...
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

May 15 '06 #3
Nic wrote:
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[u], ddeg[v]))
for u, v in edges:
print ddeg[u], ddeg[v],
print
Here's how I would do it:

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

(all untested)

Peter
May 15 '06 #4
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" <__*******@web.de> ha scritto nel messaggio
news:e4*************@news.t-online.com...
Nic wrote:
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[u], ddeg[v]))
for u, v in edges:
print ddeg[u], ddeg[v],
print
Here's how I would do it:

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

(all untested)

Peter

May 15 '06 #5
Nic wrote:
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[u], ddeg[v])) for u, v in G.edges()]
edges.sort()
for a, b in edges:
****print*a,*b,
print

Peter
May 15 '06 #6
Nic
Perfect. Thanks.
Nic

"Peter Otten" <__*******@web.de> ha scritto nel messaggio
news:e4*************@news.t-online.com...
Nic wrote:
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[u], ddeg[v])) for u, v in G.edges()]
edges.sort()
for a, b in edges:
print a, b,
print

Peter

May 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matt Fletcher | last post by:
Hi guys, I am trying to allow the models in a mysql database to be ordered by the site owner. I was thinking along the lines of a <SELECT> list containing the model names and Up and Down...
5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
18
by: Makis Papapanagiotou | last post by:
Hello all, There is a strange case where a class Test is composed from three objects of other classes. i.e. class Test { public: Test(); private: Point x;
33
by: Jim Cobban | last post by:
I cannot get Netscape 4.79 to properly display the ordered list in the following fragment. <P>Get a specific portion of the date. Depending upon the value of index: <ol start=0> <li>complete...
3
by: Charles | last post by:
I am trying to add the ability for a user to change the order in which the elements are listed in a dropdown list box. Before I added the ListID field the dropdown list box order was controlled...
3
by: sugaray | last post by:
hi, i have to build a linked-list which has another sturcture _score as it's data entry, so how can i sort such linked-list based on, let say, history score into proper order...
4
by: shrishjain | last post by:
Hi All, I need a type where I can store my items in sorted order. And I want to keep adding items to it, and want it to remain sorted. Is there any type in .net which I can make use of. I see...
3
by: idletask | last post by:
I have an application that has many windows. One window is used for order processing. I would like this window to display the other users who are also performing order processing (the idea is to...
19
beacon
by: beacon | last post by:
As you can probably tell from the title, I'm a little frustrated with linked lists. I'm working on a homework assignment and it just isn't making sense to me. I've read and read and read on the...
12
by: Richard Penfold | last post by:
I am developing an order tracking database, which to keep this explanation simple, consists of 'Orders' table, 'Order Details' table, 'Deliveries' table & 'Inventory' table. There are one-to-many...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.