我想在python中将列表填充到一定长度,所以我可以这样做:
>>> a = [1,2,3,4]
>>> b = [0,1]
>>> rng = max(len(l) for l in [a,b])
>>> for lst in [a, b]:
... apnd = [0]*(rng-len(lst))
... lst += apnd
...
>>> a
[1, 2, 3, 4]
>>> b
[0, 1, 0, 0]
但是我想说我想在那个名单前面添加:
>>> a
[1, 2, 3, 4]
>>> b
[0, 1, 0, 0]
>>> a = [1,2,3,4]
>>> b = [0,1]
>>> rng = max(len(l) for l in [a,b])
>>> for lst in [a, b]:
... prpnd = [0]*(rng-len(lst))
... lst = prpnd + lst
...
>>> a
[1, 2, 3, 4]
>>> b
[0, 1]
这不起作用。为什么' + ='操作员在for循环中工作,但是' ='运营商没有?
我可以在这个for循环中的列表前面添加什么方法?