[英]Erlang - Split a list into lists based on value
I am trying to split this list 我正在尝试拆分此列表
List = [[<<"5">>, <<"54">>], [<<"00">>], [<<"35">>, <<"54">>, <<"45">>, <<"55">>], [<<"00">>],[ <<"5">>]]
into 成
List = [[<<"5">>, <<"54">>], [<<"35">>, <<"54">>, <<"45">>, <<"55">>], [<<"5">>]]
Basically based on the <<"00">> 基本上基于<<“ 00” >>
What is the best BIF to go about this, I have some code, but its sloppy, and Im trying to learn. 最好的BIF解决方案是什么,我有一些代码,但是它草率,我正在尝试学习。
Thanks 谢谢
EDIT: 编辑:
Tried the following, does not work 尝试以下方法,不起作用
Done2 = lists:splitwith( [<<"00">>], Done1), Done2 =列表:splitwith([<<“ 00” >>],Done1),
EDIT: This Line works! 编辑:这行有效!
7> lists:splitwith(fun(A) -> A == [<<"00">>] end, List).
{[],
[[<<"5">>,<<"54">>],
[<<"00">>],
[<<"35">>,<<"54">>,<<"45">>,<<"55">>],
[<<"00">>],
[<<"5">>]]}
However I need something a little more complicated: like when the delim is [<<"00">>,<<"23">>] 但是,我需要一些更复杂的东西:比如当delim为[<<“ 00” >>,<<“ 23” >>]时
9> List = [[<<"5">>,<<"54">>], [<<"00">>,<<"23">>], [<<"35">>,<<"54">>], [<<"5">
>], [<<"00">>, <<"23">>]].
[[<<"5">>,<<"54">>],
[<<"00">>,<<"23">>],
[<<"35">>,<<"54">>],
[<<"5">>],
[<<"00">>,<<"23">>]]
10> lists:splitwith(fun(A) -> A == [<<"00">>] end, List).
{[],
[[<<"5">>,<<"54">>],
[<<"00">>,<<"23">>],
[<<"35">>,<<"54">>],
[<<"5">>],
[<<"00">>,<<"23">>]]}
11> lists:splitwith(fun(A) -> A == [<<"00">>,<<"23">>] end, List).
{[],
[[<<"5">>,<<"54">>],
[<<"00">>,<<"23">>],
[<<"35">>,<<"54">>],
[<<"5">>],
[<<"00">>,<<"23">>]]}
12>
No luck there 那里没有运气
I am not sure if I understood your requirements correctly. 我不确定我是否正确理解您的要求。 Here is a possible solution.
这是一个可能的解决方案。 It will split the list on any delimiter like [<<"00">> |
它将在[[<<“ 00” >> | _ ].
_]。
1> List = [[<<"5">>, <<"54">>], [<<"00">>], [<<"35">>, <<"54">>, <<"45">>, <<"55">>], [<<"00">>, <<"23">> ],[ <<"5">>]].
[[<<"5">>,<<"54">>],
[<<"00">>],
[<<"35">>,<<"54">>,<<"45">>,<<"55">>],
[<<"00">>,<<"23">>],
[<<"5">>]]
2> List2 = [ X || X <- List, case X of [ <<"00">> | _ ] -> false; _ -> true end].
[[<<"5">>,<<"54">>],
[<<"35">>,<<"54">>,<<"45">>,<<"55">>],
[<<"5">>]]
1> List = [[<<"5">>,<<"54">>], [<<"00">>,<<"23">>], [<<"35">>,<<"54">>], [<<"5">>], [<<"00">>, <<"23">>]]. 1>列表= [[<<“ 5” >>,<<“ 54” >>],[<<“ 00” >>,<<“ 23” >>],[<<“ 35” >>, <<“ 54” >>],[<<“ 5” >>],[<<“ 00” >>,<<“ 23” >>]]]。 [[<<"5">>,<<"54">>], [<<"00">>,<<"23">>], [<<"35">>,<<"54">>], [<<"5">>], [<<"00">>,<<"23">>]]
[[<<“ 5” >>,<<“ 54” >>],[<<“ 00” >>,<<“ 23” >>],[<<“ 35” >>,<<“ 54 “ >>],[<<” 5“ >>],[<<” 00“ >>,<<” 23“ >>]]]
2> Delimiter = [<<"00">>,<<"23">>]. 2>分隔符= [<<“ 00” >>,<<“ 23” >>]。
[<<"00">>,<<"23">>] [<< “00” >>,<< “23” >>]
3> lists:filter(fun(L) -> L /= Delimiter end, List). 3> list:filter(fun(L)-> L / =分隔符结尾,列表)。
[[<<"5">>,<<"54">>],[<<"35">>,<<"54">>],[<<"5">>]] [[<< “5” >>,<< “54” >>],[<< “35” >>,<< “54” >>],[<< “5” >>]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.