Combine/Merge Two Erlang lists

2019-06-22 18:14发布

How to combine tuple lists in erlang? I have lists:

L1 = [{k1, 10}, {k2, 20}, {k3, 30}, {k4, 20.9}, {k6, "Hello world"}],

and

L2 = [{k1, 90}, {k2, 210}, {k3, 60}, {k4, 66.9}, {k6, "Hello universe"}],

now I want a combined list as :

L3 = [
       {k1, [10, 90]},
       {k2, [20, 210]},
       {K3, [30, 60]},
       {k4, [20.9, 66.9]},
       {K6, ["Hello world", "Hello universe"]}
     ].

标签: erlang
5条回答
我只想做你的唯一
2楼-- · 2019-06-22 18:28

What happened to lists:zipwith/2?

Assumptions:

  • lists are the same length
  • lists contain the same keys in the same order

lists:zipwith(fun({X, Y}, {X, Z}) -> {X, [Y, Z]} end, L1, L2).

查看更多
疯言疯语
3楼-- · 2019-06-22 18:34

Something shorter, and the lists don't even have to posses the same keys, and can be unordered:

merge(In1,In2) ->
    Combined = In1 ++ In2,
    Fun      = fun(Key) -> {Key,proplists:get_all_values(Key,Combined)} end,
    lists:map(Fun,proplists:get_keys(Combined)).

Fun could be written directly in the lists:map/2 function, but this makes it readable.

Output, with data from example:

1> test:merge(L1,L2).
[{k1,"\nZ"},
 {k2,[20,210]},
 {k3,[30,60]},
 {k4,[20.9,66.9]},
 {k6,["Hello world","Hello universe"]}]

"\nZ" is because erlang interprets [10,90] as a string (which are, in fact, lists). Don't bother.

查看更多
家丑人穷心不美
4楼-- · 2019-06-22 18:34

This technique is called merge join. It is well known in database design.

merge(L1, L2) ->
    merge_(lists:sort(L1), lists:sort(L2)).

merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([], []) -> [].

If there can be different sets of keys in both lists and you are willing to drop those values you can use

merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([{K1, _}|T1], [{K2, _}|_]=L2) when K1 < K2 -> merge_(T1, L2);
merge_(L1, [{_, _}|T2]) -> merge_(L1, T2);`
merge_(_, []) -> [].

Or if you would like store those values in lists

merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([{K1, V1}|T1], [{K2, _}|_]=L2) when K1 < K2 -> [{K1, [V1]}|merge_(T1, L2)];
merge_(L1, [{K2, V2}|T2]) -> [{K2, [V2]}|merge_(L1, T2)];
merge_(L1, []) -> [{K, [V]} || {K, V} <- L1].

You can of course use tail recursive version if you don't mind result in reverse order or you can always use lists:reverse/1

merge(L1, L2) ->
    merge(lists:sort(L1), lists:sort(L2), []).

merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([], [], Acc) -> Acc. % or lists:reverse(Acc).

Or

merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([{K1, _}|T1], [{K2, _}|_]=L2, Acc) when K1 < K2 -> merge(T1, L2, Acc);
merge(L1, [{_, _}|T2], Acc) -> merge(L1, T2, Acc);`
merge(_, [], Acc) -> Acc. % or lists:reverse(Acc).

Or

merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([{K1, V1}|T1], [{K2, _}|_]=L2, Acc) when K1 < K2 -> merge(T1, L2, [{K1, [V1]}|Acc]);
merge(L1, [{K2, V2}|T2], Acc) -> merge(L1, T2, [{K2, [V2]}|Acc]);`
merge([{K1, V1}|T1], [], Acc) -> merge(T1, [], [{K1, [V1]} | Acc]);
merge([], [], Acc) -> Acc. % or lists:reverse(Acc).
% or merge(L1, [], Acc) -> lists:reverse(Acc, [{K, [V]} || {K, V} <- L1]).
% instead of two last clauses.

If there is possibility that one of lists can contain same keys and you are willing collect all values you can consider this

merge(L1, L2) ->
    merge(lists:sort(L1), lists:sort(L2), []).

merge([{K1, _}|_]=L1, {K2, _}|_]=L2, Acc) ->
    K = min(K1, K2),
    {Vs1, T1} = collect(K, L1, []),
    {Vs2, T2} = collect(K, L2, Vs1),
    merge(T1, T2, [{K, Vs2}|Acc]);
merge([{K, _}|_]=L1, [], Acc) ->
    {Vs, T1} = collect(K, L1, []),
    merge(T1, [], [{K, Vs}|Acc]);
merge([], [{K, _}|_]=L2, Acc) ->
    {Vs, T2} = collect(K, L2, []),
    merge([], T2, [{K, Vs}|Acc]);
merge([], [], Acc) -> lists:reverse(Acc).

collect(K, [{K, V}|T], Acc) -> collect(K, T, [V|Acc]);
collect(_, T, Acc) -> {Acc, T}.
查看更多
孤傲高冷的网名
5楼-- · 2019-06-22 18:39

Maybe this is not the best way, but it does what you are trying to achieve.

merge([{A, X}| T1], [{A, Y} | T2], Acc) ->
    New_acc = [{A, [X, Y]} | Acc],
    merge(T1, T2, New_acc);

merge([{A, X} | T1], [{B, Y} | T2], Acc) ->
    New_acc = [{A, [X]}, {B, Y} | Acc],
    merge(T1, T2, New_acc);

merge([], [{B, Y} | T], Acc) ->
    New_acc = [{B, Y} | Acc],
    merge([], T, New_acc);

merge([{A, X} | T], [], Acc) ->
    New_acc = [{A, X} | Acc],
    merge(T, [], New_acc);

merge([], [], Acc) ->
    lists:reverse(Acc).

Edit I'm assuming that the input lists are ordered as in your sample input. If not you can use lists:sort/2 to sort them before merging.

查看更多
Root(大扎)
6楼-- · 2019-06-22 18:48

There is a nice solution to this one by using the sofs module in the Erlang Standard library. The sofs module describes a DSL for working with mathematical sets. This is one of those situations, where you can utilize it by transforming your data into the SOFS-world, manipulate them inside that world, and then transform them back again outside afterwards.

Note that I did change your L3 a bit, since sofs does not preserve the string order.

-module(z).

-compile(export_all). % Don't do this normally :)

x() ->
    L1 = [{k1, 10}, {k2, 20}, {k3, 30}, {k4, 20.9}, {k6, "Hello world"}],
    L2 = [{k1, 90}, {k2, 210}, {k3, 60}, {k4, 66.9}, {k6, "Hello universe"}],
    L3 = [{k1, [10, 90]},{k2, [20, 210]},{k3, [30, 60]},{k4, [20.9, 66.9]},{k6, ["Hello universe", "Hello world"]}],
    R = sofs:relation(L1 ++ L2),
    F = sofs:relation_to_family(R),
    L3 = sofs:to_external(F),
    ok.
查看更多
登录 后发表回答