Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang 将列表列表转换为元组列表的最佳方法?_Erlang_Erlang Shell_Erlang Escript - Fatal编程技术网

Erlang 将列表列表转换为元组列表的最佳方法?

Erlang 将列表列表转换为元组列表的最佳方法?,erlang,erlang-shell,erlang-escript,Erlang,Erlang Shell,Erlang Escript,将列表(如[[1,2,3],[a,b,c],[4,5,6]]转换为元组列表的最佳方法是什么: [{1,a,4},{2,b,5},{3,c,6}] 其中元组N由三个子列表中的每一个子列表的第N个元素组成?我应该使用尾部递归函数、列表理解还是其他方法?我更喜欢使用案例列表理解,因为: 这是一个简短的代码行,易于阅读 它不需要助手函数,因此完成的操作清晰可见 L_of_tuple=[list_to_tuple(X)| | X例如: L = [[1,2,3],[a,b,c],[4,5,6]]. S

将列表(如
[[1,2,3],[a,b,c],[4,5,6]]
转换为元组列表的最佳方法是什么:

[{1,a,4},{2,b,5},{3,c,6}]

其中元组N由三个子列表中的每一个子列表的第N个元素组成?我应该使用尾部递归函数、列表理解还是其他方法?

我更喜欢使用案例列表理解,因为:

  • 这是一个简短的代码行,易于阅读
  • 它不需要助手函数,因此完成的操作清晰可见

    L_of_tuple=[list_to_tuple(X)| | X例如:

    L =  [[1,2,3],[a,b,c],[4,5,6]].
    Size = length(L).
    T = [fun(I)->list_to_tuple([lists:nth(I,lists:nth(J,L)) ||J<-lists:seq(1,Size)]) end(I) || I<-lists:seq(1,Size)].
    
    L=[[1,2,3],[a,b,c],[4,5,6]]。
    尺寸=长度(L)。
    
    T=[fun(I)->list_to_tuple([lists:nth(I,lists:nth(J,L))|J只需使用标准的
    lists:zip3/3
    函数:

    1> [L1,L2,L3] = [[1,2,3],[a,b,c],[4,5,6]].
    [[1,2,3],[a,b,c],[4,5,6]]
    2> lists:zip3(L1,L2,L3).
    [{1,a,4},{2,b,5},{3,c,6}]
    
    或者,如果您希望避免提取单个列表:

    3> apply(lists, zip3, [[1,2,3],[a,b,c],[4,5,6]]).
    [{1,a,4},{2,b,5},{3,c,6}]
    

    一个优雅的解决方案可以是

    lol2lot([[]|_]) -> [];
    lol2lot(LoL) ->
        [ list_to_tuple([hd(L) || L <- LoL]) | lol2lot([tl(L) || L <- LoL]) ].
    
    lol2lot([[]|.])->[];
    lol2lot(LoL)->
    
    [list_to_tuple([hd(L)| L请用上述问题的解决方案进行解释。请用上述问题的解决方案进行解释