合并列表erlang的内部列表 [英] Merge inner lists of a list erlang

查看:181
本文介绍了合并列表erlang的内部列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表 L

L = [L1, L2, L3,...]. 

其中L1,L2,L3 ..本身就是列表,如下所示:

where L1, L2, L3.. are themselves lists, like so:

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 = [...]

现在我想要一个结果组合列表:

now I want a result combined list as :

FinalList = [
       {k1, [10, 90, ...]}, % '...' denotes values from other lists
       {k2, [20, 210, ...]},
       {K3, [30, 60, ...]},
       {k4, [20.9, 66.9, ...]},
       {K6, ["Hello world", "Hello universe", ...]}
     ]

我可以使用我的旧帖子解决方案:合并/合并两个Erlang列表 但是我不知道如何将 L (列表列表)传递给该合并函数。

I can merge using my Old Posts Solutions:Combine/Merge Two Erlang lists But I'm not sure how I would pass L (a list of lists) to that merge function.

提前谢谢!

推荐答案

Berzemus从链接问题中的答案可以轻松调整:

Berzemus' answer from the linked question can be easily adapted:

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

但是,如果 L 或它们很长。在这种情况下,您应该从Vychodil的解决方案开始。

However, this will be quite inefficient if there are many lists in L or they are long. In this case you should start from Vychodil's solution.

这篇关于合并列表erlang的内部列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆