Erlang 尝试将列表的每个元素与映射的键进行比较[我尝试使用以下代码]
我尝试了下面的代码,而M1是由M1=#{a=>“Apple”,b=>“Ball”}组成的映射,Str是由用户ex:fun(“ab”)给出的 我想根据给定的字符串Str打印Map M1中键的相关值 试用代码:Erlang 尝试将列表的每个元素与映射的键进行比较[我尝试使用以下代码],erlang,Erlang,我尝试了下面的代码,而M1是由M1=#{a=>“Apple”,b=>“Ball”}组成的映射,Str是由用户ex:fun(“ab”)给出的 我想根据给定的字符串Str打印Map M1中键的相关值 试用代码: fun(Str) -> X = [ [X] || X <- Str], Key = maps:keys(M1), mOrse_convert(X,Key) end; mOrse_convert([],Key) -> true; mOrse_convert
fun(Str) ->
X = [ [X] || X <- Str],
Key = maps:keys(M1),
mOrse_convert(X,Key)
end;
mOrse_convert([],Key) ->
true;
mOrse_convert([First|Rest],Key) ->
case Key of
#{ X := A} -> A
end
mOrse_convert(Rest,Key)
end.
fun(Str)->
X=[[X]| | X
是的;
莫尔斯转换([第一个|休息],键)->
案例关键
#{X:=A}->A
结束
莫尔斯转换(静止,键)
结束。
有人能帮我/给我提建议吗?此函数有两个参数:
Str
-要查找和定位的值Map
。此函数返回Map中所有值等于Str
的键的列表
-module(ex).
-export([func/2]).
func(Str, Map) ->
Fun = fun(K, V, Acc) ->
if V == Str -> [K | Acc];
true -> Acc
end
end,
maps:fold(Fun, [], Map).
转到Erlang shell并键入以下命令:
1> c("ex.erl").
{ok,ex}
2> ex:func("a", #{1 => "a", 2 => "b", 3 => "ab"}).
[1]
3> ex:func("a", #{1 => "a", 2 => "b", 3 => "ab", 4 => "a"}).
[4,1]
此函数有两个参数:
Str
-要查找和定位的值Map
。此函数返回Map中值等于Str
的所有键的列表
-module(ex).
-export([func/2]).
func(Str, Map) ->
Fun = fun(K, V, Acc) ->
if V == Str -> [K | Acc];
true -> Acc
end
end,
maps:fold(Fun, [], Map).
转到Erlang shell并键入以下命令:
1> c("ex.erl").
{ok,ex}
2> ex:func("a", #{1 => "a", 2 => "b", 3 => "ab"}).
[1]
3> ex:func("a", #{1 => "a", 2 => "b", 3 => "ab", 4 => "a"}).
[4,1]
谢谢你,Alexei,你提供的解决方案对字母表很有效,只输出单个字符,如下所示,但我想将列表作为输入传递到Str中,我尝试了以下代码作为输入 单个字母表的输出:
20> c(morse).
{ok,morse}
21> morse:morse_convert("s").
Single ok
我需要的输出是:
20> c(morse).
{ok,morse}
21> morse:morse_convert("abc").
Apple Ball Cat ok
试用代码:
X = [ [X] || X <- Str],
Y = func(X,Morse),
io:format(Y ).
func([],{}) -> io:fwrite("\nCompleted.\n");
func([First | Last], Morse) ->
Fun = fun(K, V, Acc) ->
if K == First -> [V | Acc];
true -> Acc
end
end,
maps:fold(Fun, [], Morse),
func(Last,Morse).
X=[[X]| | X io:fwrite(“\n完成。\n”);
func([First | Last],Morse)->
乐趣=乐趣(K、V、Acc)->
如果K==First->[V|Acc];
正确->Acc
结束
完,,
地图:折叠(有趣,[],莫尔斯),
func(最后,莫尔斯)。
谢谢你,Alexei,你提供的解决方案对字母表很有效,只输出单个字符,如下所示,但我想将列表作为输入传递到Str中,我尝试了以下代码作为输入
单个字母表的输出:
20> c(morse).
{ok,morse}
21> morse:morse_convert("s").
Single ok
我需要的输出是:
20> c(morse).
{ok,morse}
21> morse:morse_convert("abc").
Apple Ball Cat ok
试用代码:
X = [ [X] || X <- Str],
Y = func(X,Morse),
io:format(Y ).
func([],{}) -> io:fwrite("\nCompleted.\n");
func([First | Last], Morse) ->
Fun = fun(K, V, Acc) ->
if K == First -> [V | Acc];
true -> Acc
end
end,
maps:fold(Fun, [], Morse),
func(Last,Morse).
X=[[X]| | X io:fwrite(“\n完成。\n”);
func([First | Last],Morse)->
乐趣=乐趣(K、V、Acc)->
如果K==First->[V|Acc];
正确->Acc
结束
完,,
地图:折叠(有趣,[],莫尔斯),
func(最后,莫尔斯)。
在外壳中:
~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
2> a:convert("abc").
Apple Ball Cat
ok
3>
erlang字符串(例如,“abc”
)实际上是创建整数列表的快捷方式,其中每个整数都是字符串中字母的ascii码。例如:
3> "abc" =:= [97,98,99].
true
当shell输出一个字符串时,shell希望您知道该字符串实际上是一个整数列表,这很容易混淆。要消除这种混淆,您可以通过执行函数shell:strings(false)
,告诉shell不要输出字符串:
然后你就可以看到你真正在处理什么了
接下来,当您使用模式匹配从列表中删除整数时,您有一个整数,而不是字符串。要从整数创建字符串,您需要将整数放入列表中:
[Int]
你可以在这里看到:
2> 97 =:= "a".
false
3> [97] =:= "a".
true
不清楚地图中是否有原子或字符串作为键,或者您是否不在乎。如果地图必须有原子作为键,您可以使用list_to_atom/1
将字符串转换为原子:
8> list_to_atom([97]).
a
最后,当您想要输出一个字符串,例如映射中的一个值,而不是一个列表时(执行shell:strings(false)
),您可以使用~s
格式序列:
16> io:format("~s~n", ["Apple"]).
Apple
ok
使用列表理解将如下所示:
convert(Str) ->
Cypher = #{"a" =>"Apple",
"b" => "Ball",
"c" => "Cat"
},
[
io:format("~s ", [maps:get([Int], Cypher)] )
|| Int <- Str
],
io:format("~n").
convert(Str)->
密码={“a”=>“苹果”,
“b”=>“球”,
“c”=>“Cat”
},
[
io:format(“~s”,[maps:get([Int],Cypher)])
||Int
在外壳中:
~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
2> a:convert("abc").
Apple Ball Cat
ok
3>
erlang字符串(例如,“abc”
)实际上是创建整数列表的快捷方式,其中每个整数都是字符串中字母的ascii码。例如:
3> "abc" =:= [97,98,99].
true
当shell输出一个字符串时,shell希望您知道该字符串实际上是一个整数列表,这很容易混淆。要消除这种混淆,您可以通过执行函数shell:strings(false)
,告诉shell不要输出字符串:
然后你就可以看到你真正在处理什么了
接下来,当您使用模式匹配从列表中删除整数时,您有一个整数,而不是字符串。要从整数创建字符串,您需要将整数放入列表中:
[Int]
你可以在这里看到:
2> 97 =:= "a".
false
3> [97] =:= "a".
true
不清楚地图中是否有原子或字符串作为键,或者您是否不在乎。如果地图必须有原子作为键,您可以使用list_to_atom/1
将字符串转换为原子:
8> list_to_atom([97]).
a
最后,当您想要输出一个字符串,例如映射中的一个值,而不是一个列表时(执行shell:strings(false)
),您可以使用~s
格式序列:
16> io:format("~s~n", ["Apple"]).
Apple
ok
使用列表理解将如下所示:
convert(Str) ->
Cypher = #{"a" =>"Apple",
"b" => "Ball",
"c" => "Cat"
},
[
io:format("~s ", [maps:get([Int], Cypher)] )
|| Int <- Str
],
io:format("~n").
convert(Str)->
密码={“a”=>“苹果”,
“b”=>“球”,
“c”=>“Cat”
},
[
io:format(“~s”,[maps:get([Int],Cypher)])
||Int我不确定你在找什么,这里有两种解释
1> M = #{a =>"Apple",b =>"Ball",c=>"Corn",d=>"Donut",e=>"Ball"}.
#{a => "Apple",b => "Ball",c => "Corn",d => "Donut",
e => "Ball"}
2> L = ["Ball","Donut"].
["Ball","Donut"]
3> % first, get list of keys from list of values: expected result = [b,d,e]
3> [K || {K,V} <- maps:to_list(M), lists:member(V,L) == true].
[b,d,e]
4> L1 = "ace".
"ace"
5> % second, get list of values from list of keys: expected result ["Apple","Corn","Ball"]
5> [maps:get(list_to_atom([X]),M) || X <- L1].
["Apple","Corn","Ball"]
6>
1>M=#{a=>“苹果”,b=>“球”,c=>“玉米”,d=>“甜甜圈”,e=>“球”}。
#{a=>“苹果”,b=>“球”,c=>“玉米”,d=>“甜甜圈”,
e=>“球”}
2> L=[“球”,“甜甜圈”]。
[“球”、“甜甜圈”]
3> %首先,从值列表中获取键列表:预期结果=[b,d,e]
3> [K |{K,V}L1=“ace”。
“王牌”
5> %秒,从键列表中获取值列表:预期结果[“苹果”、“玉米”、“球”]
5> [maps:get(list_to_atom([X]),M)| X
我不确定你在找什么,这里有两种解释
1> M = #{a =>"Apple",b =>"Ball",c=>"Corn",d=>"Donut",e=>"Ball"}.
#{a => "Apple",b => "Ball",c => "Corn",d => "Donut",
e => "Ball"}
2> L = ["Ball","Donut"].
["Ball","Donut"]
3> % first, get list of keys from list of values: expected result = [b,d,e]
3> [K || {K,V} <- maps:to_list(M), lists:member(V,L) == true].
[b,d,e]
4> L1 = "ace".
"ace"
5> % second, get list of values from list of keys: expected result ["Apple","Corn","Ball"]
5> [maps:get(list_to_atom([X]),M) || X <- L1].
["Apple","Corn","Ball"]
6>
1>M=#{a=>“苹果”,b=>“球”,c=>“玉米”,d=>“甜甜圈”,e=>“球”}。
#{a=>“苹果”,b