如何在erlang中组合元组列表?我有名单:
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 world", "Hello universe"]}
].
答案 0 :(得分:5)
通过使用Erlang标准库中的sofs
模块,可以很好地解决这个问题。 sofs
模块描述了用于处理数学集的DSL。这是其中一种情况,您可以通过将数据转换为SOFS世界来利用它,在该世界中操纵它们,然后再将它们转换回来。
请注意,我确实更改了您的L3,因为sofs
不保留字符串顺序。
-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.
答案 1 :(得分:4)
更短的内容,列表甚至不必拥有相同的密钥,并且可以是无序的:
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可以直接写在lists:map/2
函数中,但这使它可读。
输出,包含示例中的数据:
1> test:merge(L1,L2).
[{k1,"\nZ"},
{k2,[20,210]},
{k3,[30,60]},
{k4,[20.9,66.9]},
{k6,["Hello world","Hello universe"]}]
"\nZ"
是因为erlang将[10,90]解释为字符串(实际上是列表)。不要打扰。
答案 2 :(得分:4)
此技术称为合并连接。它在数据库设计中是众所周知的。
merge(L1, L2) ->
merge_(lists:sort(L1), lists:sort(L2)).
merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([], []) -> [].
如果两个列表中都有不同的键组,并且您愿意删除这些值,则可以使用
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_(_, []) -> [].
或者,如果您希望将这些值存储在列表中
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].
如果您不介意反向排序,或者您始终可以使用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).
或者
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).
或者
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.
如果其中一个列表可能包含相同的键并且您愿意收集所有值,则可以考虑使用
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}.
答案 3 :(得分:3)
lists:zipwith/2
发生了什么?
假设:
lists:zipwith(fun({X, Y}, {X, Z}) -> {X, [Y, Z]} end, L1, L2).
答案 4 :(得分:2)
也许这不是最好的方式,但它确实是你想要实现的目标。
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).
修改强>
我假设输入列表按照您的示例输入进行排序。如果不是,您可以在合并之前使用lists:sort/2
对它们进行排序。