a=[2,3,1,5,6,7]
b=[0,0,2,4,6,0]
谁能告诉我如何删除数组a中的[1,5,6]并替换为b值[2,4,6]?
答案 0 :(得分:0)
您可以使用list slicing:
逻辑:将所有非零值从b替换为a。
In [2082]: a=[2,3,1,5,6,7]
In [2083]: b=[0,0,2,4,6,0]
In [2104]: for c,i in enumerate(b):
...: if i != 0:
...: a[c] = i
...:
In [2105]: a
Out[2105]: [2, 3, 2, 4, 6, 7]
答案 1 :(得分:0)
您可以像这样使用切片:
a = [2,3,1,5,6,7]
b = [0,0,2,4,6,0]
a[2:5] = b[2:5]
print(a)
>>> [2, 3, 2, 4, 6, 7]