Extracting Unique Elements From a List
来源:互联网 发布:雪梨的淘宝店铺叫什么 编辑:程序博客网 时间:2024/05/16 10:42
Problem
You want to eliminate duplicate values from a list.
Solution
There are several possible solutions for this problem. Here are some of these:
Using lists:usort
The lists module contains a wealth of list processing functionality. One possible solution to this problem is to use the lists:usort function, which takes a list and returns a sorted copy of the original list, with all duplicates removed:
1> UL = [1,2,8,7,8,10,3,12,3,99,188,3,2,1,3,5,15,72].[1,2,8,7,8,10,3,12,3,99,188,3,2,1,3,5,15,72]2> lists:usort(UL).[1,2,3,5,7,8,10,12,15,72,99,188]
Using the sets Module
Erlang standard libraries includes a module, sets, with a variety of functions related to generating, creating, and manipulating mathematical sets.
10> Set = sets:from_list(UL).{sets,12, 16, 16, 8, 80, 48, {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}, {{[],[99,3],[],[],"/274/f",[15],[2],[5],"H/b",[],[],[1],[],[7],"/n",[]}}}11> sets:to_list(Set).[3,99,12,188,15,2,5,8,72,1,7,10]
Note that sets:to_list(sets:from_list(L)) produces an unreliably arranged list.
Using a General Balanced Tree Set (gb_set)
Erlang's standard libraries includes an implementation of Professor Arne Andersson's General Balanced Trees. These structures are more costly than sorting lists for small sets, but this is a much more efficient implementation when working with large sets of data.
The gb_set:from_list function will produce an ordered set of elements (dropping duplicates). The set can then be extracted back to a list for other use:
3> GBSet = gb_sets:from_list(UL).{12, {10, {5,{2,{1,nil,nil},{3,nil,nil}},{8,{7,nil,nil},nil}}, {72,{15,{12,nil,nil},nil},{188,{99,nil,nil},nil}}}}4> gb_sets:to_list(GBSet).[1,2,3,5,7,8,10,12,15,72,99,188]
转自:http://www.trapexit.org/Extracting_Unique_Elements_From_a_List
- Extracting Unique Elements From a List
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- Extracting Multiple Resultsets from a DataReader
- Extracting Single Images from a CImageList object
- The best way to get unique elements of a list in Python
- P23 (**) Extract a given number of randomly selected elements from a list.
- Can you remove elements from a std::list while iterating through it?
- P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list.
- Extracting DDLs from Oracle
- Extracting Metada From Files
- Pick Several Elements From a Vector Randomly
- Delete elements while iterating a list
- P14 (*) Duplicate the elements of a list.
- Extracting Datafile Blocks From ASM
- Extracting Files from RPM Packages
- Python.Extracting Data from XML
- Python.Extracting Data from JSON
- Extracting a SQL CE DB from Isolated Storage in WP7 Mango
- dwr (session error)
- Eclipse 删除 空行
- 焊接小技巧_期刊摘录
- 9-socket的实践到内核--client调用connect
- 正则表达式的学习
- Extracting Unique Elements From a List
- uml
- asp连接 Oracle
- 10-socket的实践到内核--UDP的socket数据的接收
- 实数相等的判断
- ubuntu 下安装文件总结
- vs2005开发时ASPAJAXExtSetup.msi安装失败的解决
- 如何在VC的release编译中使用断点调试
- 转,mysql的select * into