Skip to content
Advertisement

Change the output of an array

I am currently working with the Node_Coords_Section of the berlin52.tsp file

This gives the following output:

{1: [565.0, 575.0], 2: [25.0, 185.0], 3: [345.0, 750.0], 4: [945.0, 685.0], 5: [845.0, 655.0], ....}

In order to be able to use satsp, I need to change the latter in this format:

[[1, 566.0, 575.0], [2, 25.0, 185.0], [3, 345.0, 750.0], .....]

What should I do to get it in this format?

Advertisement

Answer

Use lst = [[x,*y] for x, y in dic.items()]

Here is the full code:

dic = {1: [565.0, 575.0], 2: [25.0, 185.0], 3: [345.0, 750.0], 4: [945.0, 685.0], 5: [845.0, 655.0]}

lst = [[x,*y] for x, y in dic.items()]

print(lst)

OP: [[1, 565.0, 575.0], [2, 25.0, 185.0], [3, 345.0, 750.0], [4, 945.0, 685.0], [5, 845.0, 655.0]]

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement