将嵌套列表转换为元组列表

发布于 2024-08-12 17:57:41 字数 656 浏览 4 评论 0原文

我有以下列表,

["txtvers=1","userid=3A6524D4-E31C-491D-94DD-555883B1600A","name=Jarrod Roberson","version=2"]

我想创建一个字典,其中 = 的左侧是键,右侧是值。 优选地,其中键是原子。

使用以下列表理解我得到了这个。

 KVL = [string:tokens(T,"=") || T <- TXT].

[["txtvers","1"], ["userid","3A6524D4-E31C-491D-94DD-555883B1600A"], ["name","Jarrod Roberson"], ["version","2"]]

我现在正在努力解决的是如何将嵌套列表转换为元组,以便我可以将它们发送到元组列表中 我可以将它们发送到 dict:from_list

我想要的是这样的东西

[{txtvers,"1"}, {userid,"3A6524D4-E31C-491D-94DD-555883B1600A"}, {name,"Jarrod Roberson"}, {version,"2"}]

我知道必须有一种简洁的方法来做到这一点,但我就是无法理解它。

I have the following list

["txtvers=1","userid=3A6524D4-E31C-491D-94DD-555883B1600A","name=Jarrod Roberson","version=2"]

I want to create a Dict where the left side of the = is the key and the right side is the value.
Preferably where the key is an atom.

Using the following list comprehension I get this.

 KVL = [string:tokens(T,"=") || T <- TXT].

[["txtvers","1"], ["userid","3A6524D4-E31C-491D-94DD-555883B1600A"], ["name","Jarrod Roberson"], ["version","2"]]

what I am struggling with now is how to convert the nested lists into tuples so I can send them into a list of tuples
where I can send them into dict:from_list

what I want is something like this

[{txtvers,"1"}, {userid,"3A6524D4-E31C-491D-94DD-555883B1600A"}, {name,"Jarrod Roberson"}, {version,"2"}]

I know there has to be a concise way to do this but I just can't get my head around it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦初醒的夏天 2024-08-19 17:57:41
KVL = [begin [K,V]=string:tokens(T,"="), {list_to_atom(K), V} end || T <- L]. 

;)

KVL = [begin [K,V]=string:tokens(T,"="), {list_to_atom(K), V} end || T <- L]. 

;)

夕嗳→ 2024-08-19 17:57:41

对其他人从这个问题中得到暗示的一点免责声明。使用 list_to_existing_atom 将列表转换为原子始终是一个好主意。

split_keyvalue(Str) ->
  try 
    {K, [$=|V]} = lists:splitwith(fun(X) -> X =/= $= end, Str),
    {erlang:list_to_existing_atom(K), V} 
  catch 
     error:badarg -> 
       fail 
  end.

split_keyvalues(List) ->
  [KV || {_,_}=KV <- lists:map(fun split_keyvalue/1, List)].

原因是,如果(恶意)用户提供的数据可以创建数百万个独特的原子,则可能是 DoS 攻击。唯一原子表最多有 1600 万个原子左右。

此外,标记会拆分字符串中的每个等号。只在第一个上分开不是更好吗?

A little disclaimer on anyone else taking hints from this question. It is always a good idea to turn lists into atoms using list_to_existing_atom.

split_keyvalue(Str) ->
  try 
    {K, [$=|V]} = lists:splitwith(fun(X) -> X =/= $= end, Str),
    {erlang:list_to_existing_atom(K), V} 
  catch 
     error:badarg -> 
       fail 
  end.

split_keyvalues(List) ->
  [KV || {_,_}=KV <- lists:map(fun split_keyvalue/1, List)].

The reason is that it is a possible DoS attack if (malicious) user supplied data can create million and millions of unique atoms. The table of unique atoms is max 16 million atoms big or so.

Also, tokens splits every equal sign in the string. Isnt it better to split on the first one only?

江挽川 2024-08-19 17:57:41

甚至更短:

KVL = [{list_to_atom(K), V} || [K,V] <- [string:tokens(T,"=") || T <- L]].

Even shorter:

KVL = [{list_to_atom(K), V} || [K,V] <- [string:tokens(T,"=") || T <- L]].
夜光 2024-08-19 17:57:41

我终于让它工作了!

A = [ string:tokens(KV,"=") || KV <- TXT].
[["txtvers","1"],
 ["userid","3A6524D4-E31C-491D-94DD-555883B1600A"],
 ["name","Jarrod Roberson"],
 ["version","2"]]
B = [{list_to_atom(K),V} || [K|[V|_]] <- A].
[{txtvers,"1"},
 {userid,"3A6524D4-E31C-491D-94DD-555883B1600A"},
 {name,"Jarrod Roberson"},
 {version,"2"}]

I actually got it to work finally!

A = [ string:tokens(KV,"=") || KV <- TXT].
[["txtvers","1"],
 ["userid","3A6524D4-E31C-491D-94DD-555883B1600A"],
 ["name","Jarrod Roberson"],
 ["version","2"]]
B = [{list_to_atom(K),V} || [K|[V|_]] <- A].
[{txtvers,"1"},
 {userid,"3A6524D4-E31C-491D-94DD-555883B1600A"},
 {name,"Jarrod Roberson"},
 {version,"2"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文