gpt4 book ai didi

list - 如何将列表列表与列表列表结合起来

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

我有列表列表,需要将其与另一个列表列表合并。输入示例: 答:[[1,2],[3,4],[5,6],[7,8]] B: [[1,2],[3,4],[5,6],[7,8]]

输出示例: [[1,2,1,2],[1,2,3,4],..,[7,8,5,6],[7,8,7,8]]

2 个列表的列表,其中都有 4 个列表,这将返回一个大小为 4*4 = 16 的列表列表

我尝试过递归地组合列表,但我知道即使通过也行不通。

  mergeAll [[]] [[]] = [[]]
mergeAll [[]] b = b
mergeAll a [[]] = a
mergeAll xs ys = mergeAll (merge xs ys) (drop 1 ys)

merge :: [[a]] -> [[a]] -> [[a]]
merge [[]] [[]] = [[]]
merge xs [[]] = xs
merge [[]] ys = ys
merge (x:xs) (y:ys) = ((x++y):xs)

最佳答案

您可以使用列表理解:

[ xs ++ ys | xs <- listOfLists1, ys <- listOfLists2 ]

关于list - 如何将列表列表与列表列表结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718516/

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