把元组转换成proupists [英] Convert tuples to proplists

查看:114
本文介绍了把元组转换成proupists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从MongoDB转换元组

How can I convert tuple from MongoDB

{'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}

[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]


推荐答案

假设你想基本上将元组的两个连续元素组合在一起,这不是太难了。您可以使用 element\2 从元组中拉元素。而 tuple_size\1 获取元组的大小。以下是几种处理方法:

Assuming you want to basically combine the two consecutive elements of the tuple together, this isn't too hard. You can use element\2 to pull elements from tuples. And tuple_size\1 to get the size of the tuple. Here's a couple of ways to handle this:

1> Tup = {'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}.
{'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}
2> Size = tuple_size(Tup).            
6

您可以使用列表解析:

3> [{element(X, Tup), element(X+1, Tup)} || X <- lists:seq(1, Size, 2)].
[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]

或者你可以压缩它:

4> lists:zip([element(X, Tup) || X <- lists:seq(1, Size, 2)], [element(X, Tup) || X <- lists:seq(2, Size, 2)]).
[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]

您可以通过执行拉出元素的功能来清理该zip。

You can clean up that zip by making a function to handle pulling elements out.

slice(Tuple, Start, Stop, Step) ->
    [element(N, Tuple) || N <- lists:seq(Start, Stop, Step)].

然后调用此函数:

5> lists:zip(slice(Tup, 1, Size, 2), Slice(Tup, 2, Size, 2)).
[{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}]

这篇关于把元组转换成proupists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆