从 python 中的列表中分离特定值
Separating specific values from a list in python
我有一个列表列表(称为table):
table = [[0, 1, 2], [1, 2, 1], [2, 3, 2], [0, 2, 3], [0, 3, 2], [1, 3, 3]]
其中每个子列表的第一个元素是起点,第二个是终点,第三个是两点之间的距离。所以例如[0,1,2]表示0点和1点之间的差距是2.
现在我想使用 table 中的信息来构造另一个列表列表(称为距离),其中包含所有点之间的所有距离。例如,我可以调用 distances[0][2](意思是我想要点 0 和点 2 之间的距离,所以输出应该是 3)。
但是我无法正确地将日期与 table 分开并将其放入距离中。
到目前为止我的代码是这样的:
dstFromOnePoint = []
distances = []
numberOfPoints = 4 #0,1,2,3
for i in range(numberOfPoints): #loops through all start points
for j in range(numberOfPoints): # loops through all endpoints
for val in table:
if (val[0] == i and val[1] == j) or (val[0] == j and val[1] == i): # checks start node , end node
dst = val[2]
dstFromOnePoint.append(dst)
elif (i==j): #distance from one point to itself will be 0
dstFromOnePoint.append(0)
distances.append(dstFromOnePoint)
print(distances)
print(distances[0][2])
我得到的输出:
[[0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0]]
0
我应该得到的输出:
[[0,2,3,2], [2,0,1,3], [3,1,0,2], [2,3,2,0]]
3
我认为我使用的循环不正确,因为我最终多次附加同一个列表,但即使只是查看一个单独的子列表,我也没有正确的值,所以我可能有更多问题我不知道如何解决?
下面是一个正确且更紧凑的解决方案:
numberOfPoints = 4
distances = [numberOfPoints * [0] for _ in range(numberOfPoints)]
for x, y, d in table:
distances[x][y] = d
distances[y][x] = d
您需要预先填充距离矩阵,然后按 [i][j]
分配
修复您的原始解决方案:
table = [[0, 1, 2], [1, 2, 1], [2, 3, 2], [0, 2, 3], [0, 3, 2], [1, 3, 3]]
distances = []
numberOfPoints = 4 #0,1,2,3
distances = [[0 for _ in range(numberOfPoints)] for _ in range(numberOfPoints)]
for i in range(numberOfPoints): #loops through all start points
for j in range(numberOfPoints): # loops through all endpoints
for val in table:
if (val[0] == i and val[1] == j) or (val[0] == j and val[1] == i): # checks start node , end node
dst = val[2]
distances[i][j] = dst
# actually you can drop this elif as distances is intitalized to 0
elif (i==j): #distance from one point to itself will be 0
# dstFromOnePoint.append(0)
distances[i][j] = 0
# distances.append(dstFromOnePoint)
print(distances)
print(distances[0][2])
给出:
[[0, 2, 3, 2], [2, 0, 1, 3], [3, 1, 0, 2], [2, 3, 2, 0]]
3
这里有一行使用 itertools.groupby
:
from itertools import groupby
[[next(g)[2] if j!=i else 0 for j in range(len(table[0]) + 1 )] for i, (k, g) in enumerate(groupby(sorted(e for f, s, t in table for e in [[f, s, t], [s, f, t]]), key=lambda x: x[0]))]
输出:
[[0, 2, 3, 2], [2, 0, 1, 3], [3, 1, 0, 2], [2, 3, 2, 0]]
我有一个列表列表(称为table):
table = [[0, 1, 2], [1, 2, 1], [2, 3, 2], [0, 2, 3], [0, 3, 2], [1, 3, 3]]
其中每个子列表的第一个元素是起点,第二个是终点,第三个是两点之间的距离。所以例如[0,1,2]表示0点和1点之间的差距是2.
现在我想使用 table 中的信息来构造另一个列表列表(称为距离),其中包含所有点之间的所有距离。例如,我可以调用 distances[0][2](意思是我想要点 0 和点 2 之间的距离,所以输出应该是 3)。
但是我无法正确地将日期与 table 分开并将其放入距离中。 到目前为止我的代码是这样的:
dstFromOnePoint = []
distances = []
numberOfPoints = 4 #0,1,2,3
for i in range(numberOfPoints): #loops through all start points
for j in range(numberOfPoints): # loops through all endpoints
for val in table:
if (val[0] == i and val[1] == j) or (val[0] == j and val[1] == i): # checks start node , end node
dst = val[2]
dstFromOnePoint.append(dst)
elif (i==j): #distance from one point to itself will be 0
dstFromOnePoint.append(0)
distances.append(dstFromOnePoint)
print(distances)
print(distances[0][2])
我得到的输出:
[[0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0, 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0]]
0
我应该得到的输出:
[[0,2,3,2], [2,0,1,3], [3,1,0,2], [2,3,2,0]]
3
我认为我使用的循环不正确,因为我最终多次附加同一个列表,但即使只是查看一个单独的子列表,我也没有正确的值,所以我可能有更多问题我不知道如何解决?
下面是一个正确且更紧凑的解决方案:
numberOfPoints = 4
distances = [numberOfPoints * [0] for _ in range(numberOfPoints)]
for x, y, d in table:
distances[x][y] = d
distances[y][x] = d
您需要预先填充距离矩阵,然后按 [i][j]
分配修复您的原始解决方案:
table = [[0, 1, 2], [1, 2, 1], [2, 3, 2], [0, 2, 3], [0, 3, 2], [1, 3, 3]]
distances = []
numberOfPoints = 4 #0,1,2,3
distances = [[0 for _ in range(numberOfPoints)] for _ in range(numberOfPoints)]
for i in range(numberOfPoints): #loops through all start points
for j in range(numberOfPoints): # loops through all endpoints
for val in table:
if (val[0] == i and val[1] == j) or (val[0] == j and val[1] == i): # checks start node , end node
dst = val[2]
distances[i][j] = dst
# actually you can drop this elif as distances is intitalized to 0
elif (i==j): #distance from one point to itself will be 0
# dstFromOnePoint.append(0)
distances[i][j] = 0
# distances.append(dstFromOnePoint)
print(distances)
print(distances[0][2])
给出:
[[0, 2, 3, 2], [2, 0, 1, 3], [3, 1, 0, 2], [2, 3, 2, 0]]
3
这里有一行使用 itertools.groupby
:
from itertools import groupby
[[next(g)[2] if j!=i else 0 for j in range(len(table[0]) + 1 )] for i, (k, g) in enumerate(groupby(sorted(e for f, s, t in table for e in [[f, s, t], [s, f, t]]), key=lambda x: x[0]))]
输出:
[[0, 2, 3, 2], [2, 0, 1, 3], [3, 1, 0, 2], [2, 3, 2, 0]]