gpt4 book ai didi

list - 如何将列表合并到 Elixir 中的元组列表中?

转载 作者:行者123 更新时间:2023-12-05 08:37:34 24 4
gpt4 key购买 nike

Elixir 将两个列表合并为一个元组列表的方法是什么?

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

# List of tuples from 'list1' and 'list2':

result = [{1,5}, {2,6}, {3,7}, {4,8}]

result的每个成员都是一个元组,第一个成员来自list1,第二个成员来自list2

最佳答案

您可以使用 Enum.zip/2 执行此操作:

Zips corresponding elements from a finite collection of enumerables into one list of tuples.

Enum.zip([[1, 2, 3], [:a, :b, :c], ["foo", "bar", "baz"]])
# [{1, :a, "foo"}, {2, :b, "bar"}, {3, :c, "baz"}]

Enum.zip([[1, 2, 3, 4, 5], [:a, :b, :c]])
# [{1, :a}, {2, :b}, {3, :c}]

关于list - 如何将列表合并到 Elixir 中的元组列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65333109/

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