简体   繁体   English

在列表Erlang中查找字段的位置

[英]Find the position of a field in a list Erlang

I have this list 我有这个清单

[id,container,feed_id,prev,next,feeds,name,street,street_no,
 firstname,lastname,email,password,phone...]

and I want to get the position of various elements, like name, which would be 7. 我想获取各种元素(例如name)的位置,即7。

How do I get the position of elements in a list with Erlang? 如何使用Erlang获取元素在列表中的位置?

lists:member(name, List).

This only returns whether an element is part of the list. 这仅返回元素是否为列表的一部分。

If it's likely that you want to look up the index for several items, or even all, and perhaps also do this repeatedly, it would be best to create an index mapping: 如果您可能想查找几个或什至所有项目的索引,并且可能还要重复执行一次,那么最好创建一个索引映射:

Map = maps:from_list(lists:zip(List, lists:seq(1, length(List))))

You can then keep this map around, and use it like this: 然后,您可以保留此地图,并像这样使用它:

Pos = maps:get(Element, Map)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM