Erlang列表问题
例如,我在 Erlang 中有一个包含元组的列表:
[{1, "AAA"}, {2, "AAA"}, {3, "AAAAAAAA"}]
How can i get tuple from this list with max first element from this tuples?
谢谢。
I have a list with tuples in Erlang for example:
[{1, "AAA"}, {2, "AAA"}, {3, "AAAAAAAA"}]
How can i get tuple from this list with max first element from this tuples?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下 lists:max/1 将返回您想要的内容。
为此,所有元组必须具有相同数量的元素。
In this case lists:max/1 will return what you want.
For this to work all tuples must have the same number of elements.
使用 lists:keysort/2。
<代码>1>列表:keysort(1,[{1,“AAA”},{2,“AAA”},{3,“AAAAAAAA”}])。
另一个线程
编辑:看来我读你的问题很快。如果您只想要一个包含最大值的元组,并且您的元组大小相同,则 Arjan 应该是可接受的答案。
如果您只想要一个 term() 元素包含最大值,并且 Arjan 声明的类似元组大小的规则不适用,那么我会使用
lists:foldl/3
或自己的递归函数。除非您希望对整个列表进行排序,否则不需要对整个列表进行排序。我的错误。
Use lists:keysort/2.
1> lists:keysort(1, [{1, "AAA"}, {2, "AAA"}, {3, "AAAAAAAA"}]).
Another thread
edit: Seems I read your questions to fast. If you only want one tuple containing a maximum value and your tuples are of same size Arjan should be the accepted answer.
If you only want one term() element containing a maximum value, and if the rule with similar tuple-sizes Arjan stated doesn't apply, I would go with either a
lists:foldl/3
or own recursion function.Sorting the whole list is unnecessary unless you want the whole list sorted. My mistake.