> Erlang中文手册 > keydelete/3 从元组列表里删除一个元素

lists:keydelete/3

从元组列表里删除一个元素

用法:

keydelete(Key, N, TupleList1) -> TupleList2

内部实现:

-spec keydelete(Key, N, TupleList1) -> TupleList2 when
      Key :: term(),
      N :: pos_integer(),
      TupleList1 :: [Tuple],
      TupleList2 :: [Tuple],
      Tuple :: tuple().

keydelete(K, N, L) when is_integer(N), N > 0 ->
    keydelete3(K, N, L).

keydelete3(Key, N, [H|T]) when element(N, H) == Key -> T;
keydelete3(Key, N, [H|T]) ->
    [H|keydelete3(Key, N, T)];
keydelete3(_, _, []) -> [].

从元组列表 TupleList1 里删除元组的第 N 个值跟 Key 是一样的元素,只删除第一个匹配的元素,后面有相同的不做处理, 最后返回处理过的新列表 TupleList2

TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
lists:keydelete(b, 1, TupleList).