2

Say I have:

path1 = [0,3,1]
path2 = [0, 3, 2, 1]

and I want

splitsOfPath1 = [(0,3), (3,1)]
splitsOfPath2 = [(0,3), (3, 2), (2, 1)]

How can this be achieved? the way I read paths is to go from 0 to 1, you need to visit 3. But to break that down, to go from 0 to 1. You need to go from 0 to 3 (0,3) and then from 3 to 1 (3, 1)

Cripto
  • 3,581
  • 7
  • 41
  • 65

1 Answers1

6

You can use zip and Explain Python's slice notation:

>>> path1 = [0, 3, 1]
>>> splitsOfPath1 = zip(path1, path1[1::])
>>> splitsOfPath1
[(0, 3), (3, 1)]
>>>
>>> path2 = [0, 3, 2, 1]
>>> splitsOfPath2 = zip(path2, path2[1::])
>>> splitsOfPath2
[(0, 3), (3, 2), (2, 1)]
>>>
Community
  • 1
  • 1