更新时间:2022-12-09 21:24:47
Python 列表是可变对象,这里:
Python lists are mutable objects and here:
plot_data = [[]] * len(positions)
您正在重复相同的列表 len(positions)
次.
you are repeating the same list len(positions)
times.
>>> plot_data = [[]] * 3
>>> plot_data
[[], [], []]
>>> plot_data[0].append(1)
>>> plot_data
[[1], [1], [1]]
>>>
列表中的每个列表都是对同一对象的引用.你修改一个,你会看到所有的修改.
Each list in your list is a reference to the same object. You modify one, you see the modification in all of them.
如果你想要不同的列表,你可以这样做:
If you want different lists, you can do this way:
plot_data = [[] for _ in positions]
例如:
>>> pd = [[] for _ in range(3)]
>>> pd
[[], [], []]
>>> pd[0].append(1)
>>> pd
[[1], [], []]