将嵌套列表转换为元组列表
我有以下列表,
["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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
;)
;)
对其他人从这个问题中得到暗示的一点免责声明。使用 list_to_existing_atom 将列表转换为原子始终是一个好主意。
原因是,如果(恶意)用户提供的数据可以创建数百万个独特的原子,则可能是 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.
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?
甚至更短:
Even shorter:
我终于让它工作了!
I actually got it to work finally!