Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改数组的输出_Python - Fatal编程技术网

Python 更改数组的输出

Python 更改数组的输出,python,Python,我目前正在处理berlin52.tsp文件的Node_Coords_部分 这将提供以下输出: {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], ....} 为了能够使用satsp,我需要将后者更改为以下格式: [[1, 566.0, 575.0], [2, 25.0, 185.0], [3, 345.0, 750.0], .....] 我应该怎么做才能得

我目前正在处理berlin52.tsp文件的Node_Coords_部分

这将提供以下输出:

{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], ....}
为了能够使用satsp,我需要将后者更改为以下格式:

[[1, 566.0, 575.0], [2, 25.0, 185.0], [3, 345.0, 750.0], .....]
我应该怎么做才能得到这种格式?

使用 lst=[[x,*y]表示dic.items中的x,y]

以下是完整的代码:

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)
作品:[[1565.0575.0],[2255.0185.0],[3345.0750.0],[4945.0685.0],[5845.0655.0],[5845.0655.0]。

使用 lst=[[x,*y]表示dic.items中的x,y]

以下是完整的代码:

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:[[1565.0575.0],[2255.0185.0],[3345.0750.0],[4945.0685.0],[5845.0655.0],[5845.0655.0],

您可以使用如下方法:

>>> cities = {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]}
>>> [[k] + v for k, v in cities.items()]
[[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]]
您可以使用以下类似的方法:

>>> cities = {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]}
>>> [[k] + v for k, v in cities.items()]
[[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]]

listcities.Values有效,但是我还需要包含城市的ID,在本例中为1,2,3等。字典键是否始终为整数?您需要保留原始顺序吗?是的,字典键始终是整数,并且需要优先保留原始顺序。listcities.Values有效,但是我还需要包括城市ID,在本例中为1,2,3等。是否需要字典键始终是整数?你需要保留原始顺序吗?是的,字典键总是整数,需要优先保留原始顺序。非常感谢!担任intended@pepegaClap点击答案旁边的勾号,如果这对你有效。我尝试过,但是系统不允许我:弹出这样一个错误:记录声誉低于15的人的投票,但不要更改公开显示的帖子分数。好的,完成。应该马上就来谢谢!担任intended@pepegaClap点击答案旁边的勾号,如果这对你有效。我尝试过,但是系统不允许我:弹出这样一个错误:记录声誉低于15的人的投票,但不要更改公开显示的帖子分数。好的,完成。现在应该出现了