Erlang:打印出列表中的特定值

Erlang: Printing out specific value in list

如果我有一个列表,例如:

[{list1, [1,2]},{list2, [3,4]}]

如果它作为变量传入,我将如何使用 io:format 打印出 [3,4],例如 I.

我目前在做:

io:format("list 2: ~w~n", [I]),

您的示例列表采用以下形式:[{Key1, Value1}, {Key2, Value2}, ...],其中 Key 是一个原子。这种列表也可以称为 proplist(属性 列表)。名为 proplist 的模块可以准确处理此数据结构。

在你的情况下,你可以 运行:

PList = [{list1, [1,2]},{list2, [3,4]}],
Value = proplists:get_value(list2, PList),
io:format("list2: ~p~n", [Value]).

变量 Value 现在绑定到值 [3,4]

另请参阅:The Erlang-Documentation page for proplists