2014-02-28 137 views
1

這裏是我的情況:覆蓋數組的前n個元素?

a = ["","","","","","","","","",""] #10 elements. Need not always be blank. 
b = ["a","b","c","d"] 

我想修改a使得a第一n元件用的b的元素,其中n = b.size()代替:

a = ["a","b","c","d","","","","","",""] 

那麼,有沒有一些簡單的像a.replace(b)減去截斷?

另一種方法是將a.size() - b.size()元素數加到b

回答

2

你可以用切片:

a[0, 4] = b 

或動態長度:

a[0, b.length] = b