Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a list using Networkx in Python

I have a list,

selected=[[0,5,9,10,0],[0,2,4,7,8,0],[0,1,3,6,0]]

It’s a route of 3 cars.I want to convert it below,

selected1=[(0,5),(5,9),(9,10),(10,0),(0,2),(2,4),(4,7),(7,8),(8,0),(0,1),(1,3),(3,6),(6,0)]

I can do reverse by using networkx library(selected1 to selected),but now i need selected to selected1.Thanks for everyone.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using a list comprehension might do the work :

selected=[[0,5,9,10,0],[0,2,4,7,8,0],[0,1,3,6,0]]

selected1 = [(s[i], s[i+1]) for s in selected for i in range(len(s)-1)]

# [(0, 5), (5, 9), (9, 10), (10, 0), (0, 2), (2, 4), (4, 7), (7, 8), (8, 0), (0, 1), (1, 3), (3, 6), (6, 0)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading