在 Erlang 世界中,对于固定大小的数据类型,我们使用 tuples。你的 List 可能会长大,但我认为它的元素是固定大小的,所以我建议对其元素使用元组,你可以使用模块的 API 函数lists 和 proplists:
1> List = [{"1", "Apple"}, {"2", "Orange"}, {"3", "Apple"}].
[{"1","Apple"},{"2","Orange"},{"3","Apple"}]
%% Search in List for a tuple which its 2nd element is "Apple":
2> lists:keyfind("Apple", 2, List).
{"1","Apple"}
3> lists:keyfind("Unknown", 2, List).
false
%% Take first Tuple which its 2nd element is "Apple", Also yield Rest of List:
4> lists:keytake("Apple", 2, List).
{value,{"1","Apple"},[{"2","Orange"},{"3","Apple"}]}
%% Replace a tuple which its 1st element is "3" with {"3", "Banana"}
5> lists:keyreplace("3", 1, List, {"3", "Banana"}).
[{"1","Apple"},{"2","Orange"},{"3","Banana"}]