创建具有预定义长度的 2D python 列表

时间:2021-03-14 07:45:48

标签: python python-3.x list graph

我有一个小图 enter image description here

我想像这样将节点添加到二维列表中。 enter image description here

0 连接到节点 12,因此在索引 0 处插入这两个节点,依此类推。程序应该接受用户的输入。为了简单起见,我写了这段代码。

l = [[]]*4

l[0].append(1)
l[0].append(2)

l[1].append(0)
l[1].append(3)

l[2].append(0)
l[2].append(3)

l[3].append(1)
l[3].append(2)

print(l, end="\n")

但是输出是 [[1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2]]

我想要第二张图片中的输出。如何实现?

0 个答案:

没有答案