我有两个列表x = ["a","b","c"]
和y = ["Argentina","Brazil","Canada"]
。我想要一个像[["a","Argentina"],["b","Brazil"],["c","Canada"]]
这样的列表。
谁能帮帮我吗?感谢。
答案 0 :(得分:8)
在没有看到你的用例的情况下我会猜测元组实际上足够了,并且更安全,因此这只是zip
x = ["a","b","c"]
y = ["Argentina","Brazil","Canada"]
z = zip x y
Prelude> z
[("a","Argentina"),("b","Brazil"),("c","Canada")]
答案 1 :(得分:4)
我不确定是否有更简单的方法,但您可以使用zip
生成元组列表,然后使用map
返回列表列表。
Prelude> map (\(a,b) -> [a,b]) $ zip ["a","b","c"] ["Argentina","Brazil","Canada"]
[["a","Argentina"],["b","Brazil"],["c","Canada"]]
修改:Daniel Martin指出zipWith
更简单。
Prelude> zipWith (\a b -> [a,b]) ["a","b","c"] ["Argentina","Brazil","Canada"]
[["a","Argentina"],["b","Brazil"],["c","Canada"]]
答案 2 :(得分:3)
只需使用
zipWith (\x y -> [x,y])