gpt4 book ai didi

来自两个不同大小数组的 Ruby 数组?

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

我有两个 Ruby 数组,分别是 daystrips,以及星期几:

days = ["Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday","Sunday"]

还有这里的公交时刻表:

trips = [
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm",
"2.35pm","4.50pm","7.00pm"
]

我试图达到的结果是这样的:

Bus-times = [
"Monday","2.35pm","4.50pm","7.00pm",
"Tuesday","2.35pm","4.50pm","7.00pm",
"Wednesday","2.35pm","4.50pm","7.00pm",
"Thusday","2.35pm","4.50pm","7.00pm",
"Friday","2.35pm","4.50pm","7.00pm",
"Saturday","2.35pm","4.50pm","7.00pm",
"Sunday""2.35pm","4.50pm","7.00pm"
]

我看过交错,如果我不编写自己的函数,zip 只会返回第一个结果。我还有哪些其他选择?

最佳答案

bus_times = days.zip(trips.each_slice(3)).flatten

或者如果你想将它们保存为数组的数组:

bus_times = days.zip(trips.each_slice(3)).map(&:flatten)

关于来自两个不同大小数组的 Ruby 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346569/

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