gpt4 book ai didi

ruby - 如何根据值的索引拆分数组?

转载 作者:数据小太阳 更新时间:2023-10-29 07:42:30 25 4
gpt4 key购买 nike

如何拆分具有不确定条目的数组:

["a","b","c","d","e",...]

分为偶数和奇数数组,如:

["a","c","e",...]

["b","d","f",...]

最佳答案

根据评论编辑:

arr = [:foo, :foo, :bar, :baz, :qux, :foo]

evens, odds = arr.partition.with_index{ |_, i| i.even? }

evens # [:foo, :bar, :qux]
odds # [:foo, :baz, :foo]

关于ruby - 如何根据值的索引拆分数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21336375/

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