替换Erlang列表中索引的元素

编程入门 行业动态 更新时间:2024-10-27 22:19:46
本文介绍了替换Erlang列表中索引的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个列表,需要定期更新其元素。元素没有列表的键:keyreplace。它也将动态增长。这是更新列表中特定索引的元素的好方法吗?有没有更好的算法?

I have a list that will need its elements updated periodically. The elements do not have a key for lists:keyreplace. It will also grow dynamically. Is this a good way to update an element at a specific index in a list? Is there a better algorithm?

List = [1,2,3,4], Index = 3, NewElement = 5, {HeadList, [_|TailList]} = lists:split(Index-1, List), [1,2,5,4] = lists:append([HeadList, [NewElement|TailList]]).

推荐答案

我不会建议以这种方式使用列表,这让我觉得你的问题可能与设计相关,而不是整齐地解决问题。也许如果你解释你有什么列表?

I wouldn't recommend using a list in this way, it makes me think that your problem might be design related rather than related to solving it neatly. Perhaps if you explains what you have the list for?

但是,如果这是你实际需要/想要/必须做的;那么你正在做的是正确的。

However if that is what you actually need/want/have to do; then what you are doing is correct.

我建议使用ets表或dict进行随机访问操作。

I would recommend using an ets table or dict for random access operations.

更多推荐

替换Erlang列表中索引的元素

本文发布于:2023-11-10 15:42:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   元素   列表中   Erlang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!