Python lists are mutable
当我们如下init一个list of lists时,plot_data = [[]] * len(positions)
,要注意,里面的元素是reference,于是会出现以下情形:
>> a=[[]]*5
>> a
[[], [], [], [], []]
>> a[1].append(1)
>> a
[[1], [1], [1], [1], [1]]
很可怕啊!这个bug让我de了1个半小时。正确的init方法是:a=[[] for _ in range(3)]
,这样每个list就是copy而不是reference。