将邻接表乘以 -1
Multiply adjacency list by -1
g = [
[4, 7],
[3],
[4, 5],
[4],
[2],
[0]
]
我正在尝试使用这段代码乘以上面的邻接表:
for i in range(len(g)):
for j in g[i]:
g[i] = [j * (-1) for j in g[i]]
print(g)
我得到的结果是:
[[4, 7], [-3], [4, 5], [-4], [-2], [0]]
我需要更改什么才能使所有列表都为负数?
将子列表中的每个元素乘以-1
:
g = [[4, 7], [3], [4, 5], [4], [2], [0]]
g_neg = [[v * -1 for v in subl] for subl in g]
print(g_neg)
打印:
[[-4, -7], [-3], [-4, -5], [-4], [-2], [0]]
g = [
[4, 7],
[3],
[4, 5],
[4],
[2],
[0]
]
我正在尝试使用这段代码乘以上面的邻接表:
for i in range(len(g)):
for j in g[i]:
g[i] = [j * (-1) for j in g[i]]
print(g)
我得到的结果是:
[[4, 7], [-3], [4, 5], [-4], [-2], [0]]
我需要更改什么才能使所有列表都为负数?
将子列表中的每个元素乘以-1
:
g = [[4, 7], [3], [4, 5], [4], [2], [0]]
g_neg = [[v * -1 for v in subl] for subl in g]
print(g_neg)
打印:
[[-4, -7], [-3], [-4, -5], [-4], [-2], [0]]