Home > Back-end >  How to make a list using Networkx in Python
How to make a list using Networkx in Python

Time:05-04

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.

CodePudding user response:

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)]
  • Related