gpt4 book ai didi

ruby-on-rails - Ruby zip 3 数组分两步完成

转载 作者:行者123 更新时间:2023-12-02 03:38:56 25 4
gpt4 key购买 nike

我有以下数组:

product_ids = ['id', 'id', 'id'...'id']
product_prices = ['price', 'price', 'price'...'price']

第一步我执行以下操作

list_1 = product_ids.zip(product_prices)

此时我有一个带有表单的列表

list_1 -> [['id', 'price'],['id','price']...]

经过一些计算,我得到另一个数组

warehouses_stocks = [['stock_1', 'stock_1'..],['stock_2','stock_2'...]]

如何合并这两个(list_1和warehouses_stocks)以获得另一个具有以下形式的数组

[['id','price', 'stock_1', 'stock_2'],['id','price', 'stock_1', 'stock_2'],...]

最佳答案

product_ids = ['id_1', 'id_2']
product_prices = ['price_1', 'price_2']
warehouses_stocks = [['stock_1', 'stock_1'],['stock_2','stock_2']]

zip 可以采用多个参数:

product_ids.zip(product_prices, *warehouses_stocks)
#=> [["id_1", "price_1", "stock_1", "stock_2"],
# ["id_2", "price_2", "stock_1", "stock_2"]]

关于ruby-on-rails - Ruby zip 3 数组分两步完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49289236/

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