gpt4 book ai didi

ruby - 在 Ruby 中的不同数组的特定位置从单个字符创建数组

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

myArray = ["1324","11455","2234","55345", "1234", "54321"]

如何将其分成 5 个数组,每个数组包含该数组中的 5 个字符之一?

想要的结果是

firstCharacterArray => ["1","1","2","5","1","5"]
...
fifthCharacterArray => ["","5","","5","","1"]

最佳答案

你可以这样做:

def extract_n_character(arr, n)
arr.map { |c| c[n] || '' }
end

extract_n_character(myArray, 0)
# => ["1", "1", "2", "5", "1", "5"]

extract_n_character(myArray, 4)
# => ["", "5", "", "5", "", "1"]

关于ruby - 在 Ruby 中的不同数组的特定位置从单个字符创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27032493/

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