gpt4 book ai didi

ruby - 覆盖数组的前 n 个元素?

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:40 25 4
gpt4 key购买 nike

这是我的场景:

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

最佳答案

您可以使用切片:

a[0, 4] = b

或动态长度:

a[0, b.length] = b

关于ruby - 覆盖数组的前 n 个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com