Erlang - 交错的简单方法

发布于 2024-09-13 20:19:14 字数 546 浏览 6 评论 0原文

交错三个数据集的简单/有效的方法是什么?

Data1 = [<<5>>,<<6>>,<<7>>],
Data2 = [<<5>>,<<6>>,<<7>>],
Data3 = [<<5>>,<<6>>,<<7>>].

最终结果:

Final = [<<5>>, <<5>>, <<5>>, <<6>>, <<6>>, <<6>>, <<7>>, <<7>>, <<7>>]

我确信它就像

[X || X <- [Data1, Data2, Data3]]

Whats the easy/efficient way of interleaving three data sets..

Data1 = [<<5>>,<<6>>,<<7>>],
Data2 = [<<5>>,<<6>>,<<7>>],
Data3 = [<<5>>,<<6>>,<<7>>].

End Result:

Final = [<<5>>, <<5>>, <<5>>, <<6>>, <<6>>, <<6>>, <<7>>, <<7>>, <<7>>]

Im sure its like

[X || X <- [Data1, Data2, Data3]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幼儿园老大 2024-09-20 20:19:14

模块功能:

zip3(X, Y, Z) when X =:= []; Y =:= []; Z =:= [] -> [];
zip3([HX | TX], [HY | TY], [HZ | TZ]) -> [ HX, HY, HZ | zip3(TX, TY, TZ)].

shell 中相同:

F = fun(D1, D2, D3) ->
  G = fun(F, X, Y, Z) when X =:= []; Y =:= []; Z =:= [] -> [];
         (F, [HX | TX], [HY | TY], [HZ | TZ]) -> [ HX, HY, HZ | F(F, TX, TY, TZ)]
      end,
  G(G, D1, D2, D3)
end,                                                                              
Data1 = [<<5>>,<<6>>,<<7>>],
Data2 = [<<5>>,<<6>>,<<7>>],
Data3 = [<<5>>,<<6>>,<<7>>],
F(Data1, Data2, Data3).
[<<5>>,<<5>>,<<5>>,<<6>>,<<6>>,<<6>>,<<7>>,<<7>>,<<7>>]

当然你可以使用 lists 模块:

lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)).
[<<5>>,<<5>>,<<5>>,<<6>>,<<6>>,<<6>>,<<7>>,<<7>>,<<7>>]

Module function:

zip3(X, Y, Z) when X =:= []; Y =:= []; Z =:= [] -> [];
zip3([HX | TX], [HY | TY], [HZ | TZ]) -> [ HX, HY, HZ | zip3(TX, TY, TZ)].

Same in shell:

F = fun(D1, D2, D3) ->
  G = fun(F, X, Y, Z) when X =:= []; Y =:= []; Z =:= [] -> [];
         (F, [HX | TX], [HY | TY], [HZ | TZ]) -> [ HX, HY, HZ | F(F, TX, TY, TZ)]
      end,
  G(G, D1, D2, D3)
end,                                                                              
Data1 = [<<5>>,<<6>>,<<7>>],
Data2 = [<<5>>,<<6>>,<<7>>],
Data3 = [<<5>>,<<6>>,<<7>>],
F(Data1, Data2, Data3).
[<<5>>,<<5>>,<<5>>,<<6>>,<<6>>,<<6>>,<<7>>,<<7>>,<<7>>]

And of course you can do it with lists module:

lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)).
[<<5>>,<<5>>,<<5>>,<<6>>,<<6>>,<<6>>,<<7>>,<<7>>,<<7>>]
拥醉 2024-09-20 20:19:14

您可以编写自定义 zip 函数来完成此操作。

zip([HX | TX], [HY | TY], [HZ | TZ]) -> [[HX, HY, HZ] | zip(TX, TY, TZ)];
zip([], [], []) -> [].

只要输入的长度相同,该函数就可以正常工作。处理不同长度的输入需要一些修改。像这样的东西:

zip(X, Y, Z) when length(X) =:= 0; length(Y) =:= 0; length(Z) =:= 0 -> [];
zip([HX | TX], [HY | TY], [HZ | TZ]) -> [[HX, HY, HZ] | zip(TX, TY, TZ)].

这样称呼它:

7> my_module:zip(Data1, Data2, Data3).
[[<<5>>,<<5>>,<<5>>],
 [<<6>>,<<6>>,<<6>>],
 [<<7>>,<<7>>,<<7>>]]

另请参阅:标准库函数 列表:zip3

You can write a custom zip function to accomplish this.

zip([HX | TX], [HY | TY], [HZ | TZ]) -> [[HX, HY, HZ] | zip(TX, TY, TZ)];
zip([], [], []) -> [].

This function will work fine as long as the length of the inputs are the same. Dealing with inputs of varying length will take some tinkering. Something like this:

zip(X, Y, Z) when length(X) =:= 0; length(Y) =:= 0; length(Z) =:= 0 -> [];
zip([HX | TX], [HY | TY], [HZ | TZ]) -> [[HX, HY, HZ] | zip(TX, TY, TZ)].

Call it thus:

7> my_module:zip(Data1, Data2, Data3).
[[<<5>>,<<5>>,<<5>>],
 [<<6>>,<<6>>,<<6>>],
 [<<7>>,<<7>>,<<7>>]]

See also: standard library function lists:zip3.

泪冰清 2024-09-20 20:19:14
Final = Data1 ++ Data2 ++ Data3.
Final = Data1 ++ Data2 ++ Data3.
心凉 2024-09-20 20:19:14

这是我的尝试。有了这个,您可以添加任意数量的数据集,只需将它们添加到列表中即可。它还会考虑列表的大小是否不同。如果二进制数据可能很大或者它是一个非常常见的函数,那么使用新的二进制模块可能比将二进制数据分解为 1 字节列表更有效。

-module(zippy).
-compile(export_all).

zipAll(L) -> zip({L,[]}).
zip({L,Final}) ->
    case lists:any(fun(X) -> case X of [] -> false; _ -> true end end,L) of
        true -> zip(lists:mapfoldl(fun x/2,Final,L));
        _ -> lists:reverse(Final)
    end.

x([],L) -> {[],[null|L]};
x([H|T],L) -> {T,[H|L]}.

start() ->
    Data1 = [<<5>>,<<6>>,<<7>>],
    Data2 = [<<5>>,<<6>>,<<7>>],
    Data3 = [<<5>>,<<6>>,<<7>>],
    Data4 = [<<5>>,<<6>>,<<7>>,<<1>>],
    zipAll([Data1,Data2,Data3,Data4]).

你正在考虑列表理解
[{X,Y,Z} || X<-数据1,Y<-数据2,Z<-数据3]]
这更能产生所有与顺序无关的可能性。

Here's my go at it. With this you can add as many Data sets as you want, just add them to a list. It also takes into account if the lists are different sizes. Probably more efficient to use the new binary module instead of breaking down the binary data into 1 byte lists if the binary data can be large or it's a very common function.

-module(zippy).
-compile(export_all).

zipAll(L) -> zip({L,[]}).
zip({L,Final}) ->
    case lists:any(fun(X) -> case X of [] -> false; _ -> true end end,L) of
        true -> zip(lists:mapfoldl(fun x/2,Final,L));
        _ -> lists:reverse(Final)
    end.

x([],L) -> {[],[null|L]};
x([H|T],L) -> {T,[H|L]}.

start() ->
    Data1 = [<<5>>,<<6>>,<<7>>],
    Data2 = [<<5>>,<<6>>,<<7>>],
    Data3 = [<<5>>,<<6>>,<<7>>],
    Data4 = [<<5>>,<<6>>,<<7>>,<<1>>],
    zipAll([Data1,Data2,Data3,Data4]).

you're thinking of list comprehensions
[{X,Y,Z} || X <-Data1, Y<-Data2,Z<- Data3]]
which is more to generate all possibilities where order doesn't matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文