list有setuniquelist方法吗

订阅你的位置: >
> 【整理】python中去除数组List中重复的值,即数组去重
参考代码:def unique(old_list):
newList = []
for x in old_list:
if x not in newList :
newList.append(x)
return newList转载请注明: & 与本文相关的文章
20 queries in 0.194 seconds, using 9.70MB memorytemplate & class T, class Alloc = allocator&T& &
列表(List)是一个允许在序列中任何一处位置以常量耗时插入或删除元素且可以双向迭代的顺序容器(Sequence container)。
A list is a sequence container that supports bidirectional iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically.
C++编程语言国际标准:
容性特性:
顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。
容器中的每个元素保存了定位前一个元素及后一个元素的信息,允许在任何一处位置以常量耗时进行插入或删除操作,但不能进行直接随机访问(Direct random access)。
能够感知内存分配器的(Allocator-aware)
容器使用一个内存分配器对象来动态地处理它的存储需求。
存储在容器中的元素的数据类型。
在类模板内部,使用其别名为 value_type 的成员类型。
容器内部用来管理内存分配及释放的内存分配器的类型。
这个参数是可选的,它的默认值是 std::allocator&T&,这个是一个最简单的非值依赖的(Value-independent)内存分配器。 在类模板内部,使用其别名为 allocator_type 的成员类型。
标准模板库(Standard Template Library,简称STL)中的列表容器采用的是双向链表(Doubly-linked lists)数据结构。双向链表可以保存以不同且无关的储存位置容纳的元素。元素间的顺序是通过各个元素中指向前一个元素的链接及指向后一个元素的链接等联系维护的。
容器的有点相似:主要的区别就是 std:: forward_list 是一个单向链表,因此它只能被正向迭代( Iterated forward),以换取更小更高效的特点。
增加或移动列表中的元素不会使指向它们的指针、引用、迭代器失效,只有当对应的元素被销毁时才会如此。
你可能会好奇,为什么STL同时提供列表(std::list)跟向量()及其它等多种容器,原因是它们的底层实现方式是不一样的,每一种实现方式都有各自的消耗(空间或时间)及优点。
相比较于其它的基本标准顺序容器(数组 、向量 、双端队列
等),列表通常在容器中已获得迭代器的任意位置处插入、获得(Extracting,提取)、移动元素等操作中表现出更出色的性能,对那些密集使用上述操作的算法,使用列表同样能提升性能,比如排序算法。
Since lists allow fast insertion and erasing from the middle of a list, certain operations are provided specifically for them.
C++编程语言国际标准:
双向链表()及正向链表( )相比于其它顺序容器的主要缺点是它们不能够通过元素在容器中的位置直接访问(Direct access)元素。举个例子:如果想访问一个列表中的第六个元素,那么就必须从一个已知位置(比如开头或末尾)处开始迭代,这会消耗与两个位置之间距间相关的线性时间。而且它们还为保存各个元素间的链接信息消耗更多额外的内存(这点对由小尺寸元素组成的大列表尤为明显)。
在定义一个列表对象时,所有你需要做的就是给出保存在列表中的元素的类型(更多定义方式可以参考
成员函数)。举个例子,下述代码定义了一个整数列表:
std::list&int& integer_
模板类中提供了成员函数
及 ,你可以分别用它们在列表头或尾插入一个新的元素。举个例子:
std::list&int& integer_
integer_list.push_front(1);
integer_list.push_front(2);
这个例子创建了一个列表,其中有两个元素,元素 1 及之后的元素 2。
开始,你也可以使用
分别向列表头或尾插入一个新的元素。
相对地,你也可以通过使用模板类提供的
成员函数来分别删除第一个或最后一个元素。单独地使用其中的部份函数,就能很容易的创建基于列表的队列(Queue)或栈(Stack)等数据结构,当然,最好还是使用由标准模板库提供的容器适配器
快速的在容器中间部份插入元素是列表的最大优势。可以使用成员函数
完成这一操作:需要指定插入的元素及指向插入位置的迭代器(新的元素将会插入到当前所指向元素的前面)。从
开始,你也可以使用
来完成这一操作,具体区别请查看两个函数的详细介绍。以下是 list::insert 的声明:
iterator insert(iterator position, const T& element_to_insert);
其中迭代器可以按以下方式定义:
list&type&::iterator iterator_ // type要替换成元素的具体类型
所要注意的是标准模板库中的列表同时支持正向(Forward)及反向(Reverse)迭代(因为它是基于双链表实现的)。
使用 insert 及
函数可以实现
(及 )的功能:
std::list&int& integer_
integer_list.insert(integer_list.end(), item);
正如所料的,list 类模板包含了由标准容器定义的
函数。这里有一点必须要注意:所以,如果你想简单判断一个列表是否为空,可以使用 empty 而不是 检查它的大小是否为 0。如果希望保证当前列表是空的,可以使用
函数。具体例子如下:
std::list&int&
// 会让人困惑,即使C++11有所改进
if(foo.size() == 0)
// 推荐的方法
if(foo.empty())
foo.clear();
// 现在,foo 是一个空列表
成员函数对列表进行排序,该操作时间复杂度保证为 O(nlogn)。
sort 函数的代码示例:
foo.sort();
成员函数来反转列表。标准C++库的算法库中存在一个相似的函数:,该函数同样用于反转容器中的元素。当前成员函数(list::reverse)与 std::reverse 的区别就是调用 list::reverse 后不会影响其它正在使用的迭代器所指向的值。
reverse 成员函数的代码示例:
foo.reverse()
成员函数来“唯一化”元素。该函数删除容器中的所有连续重复元素,仅仅留下每组等值元素中的第一个元素。比如,有如下元素组成列表:
在调用 unique 后,剩余的元素为:
注意上述结果中,仍然有两个 8:仅仅删除连续的等值元素。如果希望一个元素在序列中仅仅出现一次,你需要先排序这个列表。具体例子如下:
std::list&int& int_
int_list.push_back(1);
int_list.push_back(1);
int_list.push_back(8);
int_list.push_back(9);
int_list.push_back(7);
int_list.push_back(8);
int_list.push_back(2);
int_list.push_back(3);
int_list.push_back(3);
int_list.sort();
int_list.unique();
for(std::list&int&::iterator list_iter = int_list.begin();
list_iter != int_list.end(); list_iter++)
std::cout&&*list_iter&&
value_type
第一个模板参数 T
allocator_type
第二个模板参数 Alloc
无符号整数类型(通常为 )
difference_type
有符号整数类型(通常为 )
Allocator::reference
value_type&
const_reference
Allocator::const_reference
const value_type&
Allocator::pointer
&Allocator&::pointer
const_pointer
Allocator::const_pointer
&Allocator&::const_pointer
双向迭代器
const_iterator
常双向迭代器
reverse_iterator
&iterator&
const_reverse_iterator
std::reverse_iterator&const_iterator&
Iterators:
返回指向容器起始位置的迭代器()
返回指向容器末尾位置的迭代器
返回指向容器逆序起始位置的逆序迭代器()
返回指向容器逆序末尾位置的逆序迭代器
返回指向容器起始位置的常迭代器()
返回指向容器末尾位置的常迭代器
返回指向容器逆序起始位置的常逆序迭代器()
返回指向容器逆序末尾位置的常逆序迭代器
判断是否为空
返回有效元素个数
返回 list 支持的最大元素个数
Element access:
访问第一个元素
访问最后一个元素
Modifiers:
将新的内容赋值给容器
在容器开头构造及插入一个元素
在容器开头插入一个元素
删除第一个元素
在容器末尾构造及插入一个元素
在末尾增加一个元素
删除最后一个元素
构造及插入一个元素
改变有效元素的个数
Operations:
使元素从一个列表移动到另一个列表
删除值为指定值的元素
删除满足指定条件的元素
删除重复值
合并已排序的列表
为容器中的所有元素排序
反转元素的顺序
Observers:
获得内存分配器
operator==、operator!=、operator&、operator&=、operator&、operator&=
关系操作符
交换两个列表的内容
头文件中包含大量与 list 容器相关的算法,常使用的有搜索算法、、、、、、、、、、二分查找(Binary search)、、、集合(Set)操作、、、、最大与最小、字典序比较排列生成器、其它操作、、、、、、、、、、、、、、
、、、、、、、、、、、、、、、、、、、、
综合下面这个例子简单地介绍了 list 的常用使用方法。#include
namespace ClassFoo{
//创建一个list容器的实例LISTINT
typedef list&int& LISTINT;
//创建一个list容器的实例LISTCHAR
typedef list&int& LISTCHAR;
void ListExample1(){
//--------------------------
//用list容器处理整型数据
//--------------------------
//用LISTINT创建一个名为foo1的list对象
LISTINT foo1;
//声明i为迭代器
//从前面向foo1容器中添加数据
foo1.push_front (2);
foo1.push_front (1);
//从后面向foo1容器中添加数据
foo1.push_back (3);
foo1.push_back (4);
//从前向后显示foo1中的数据
cout&&"foo1.begin()--- foo1.end():"&&
for (i = foo1.begin(); i != foo1.end(); ++i)
cout && *i && " ";
//从后向后显示foo1中的数据
LISTINT::reverse_
cout&&"foo1.rbegin()---foo1.rend():"&&
for (ir =foo1.rbegin(); ir!=foo1.rend();ir++) {
cout && *ir && " ";
//使用STL的accumulate(累加)算法
int result = accumulate(foo1.begin(), foo1.end(),0);
cout&&"Sum="&&result&&
cout&&"------------------"&&
//--------------------------
//用list容器处理字符型数据
//--------------------------
//用LISTCHAR创建一个名为foo1的list对象
LISTCHAR foo2;
//声明i为迭代器
LISTCHAR::
//从前面向foo2容器中添加数据
foo2.push_front ('A');
foo2.push_front ('B');
//从后面向foo2容器中添加数据
foo2.push_back ('x');
foo2.push_back ('y');
//从前向后显示foo2中的数据
cout&&"foo2.begin()---foo2.end():"&&
for (j = foo2.begin(); j != foo2.end(); ++j)
cout && char(*j) && " ";
//使用STL的max_element算法求foo2中的最大元素并显示
j=max_element(foo2.begin(),foo2.end());
cout && "The maximum element in foo2 is: "&&char(*j)&&
int main()
ClassFoo::ListExample1();
} 输出:用 list 实现的简单的对象池#include
namespace ClassFoo{
template&class T&
class object_pool
list&void *& data_
void* malloc_data()
if(!data_list.empty())
list&void *&::iterator it = data_list.begin();
void *p = *
data_list.pop_front();
void* p = malloc(sizeof(T));
void free_data(void* p)
data_list.push_back(p);
A(int a):value(a){cout&&"A("&&a&&") called"&&}
~A() {cout&&"~A("&&value&&") called"&&}
static object_pool&A& pool_;
void* operator new (size_t size)
return pool_.malloc_data();
void operator delete(void* p)
pool_.free_data(p);
object_pool&A& A::pool_;
void ListExample2() {
A* a1=new A(1);
A* a2=new A(2);
delete a1;
delete a2;
void main()
ClassFoo::ListExample2();
}输出:排序自定义结构体对象列表#include
#define CLASSFOO_LIST(type, name, ...) \
static const type name##_a[] = __VA_ARGS__; \
std::list&type& name(name##_a, name##_a + sizeof(name##_a) / sizeof(*name##_a))
namespace ClassFoo{
struct ClassFoo {
bool compare(ClassFoo first, ClassFoo second)
if (first.i & second.i)
void ListExample3(){
CLASSFOO_LIST(ClassFoo,foo,{{2,"我"},{1,"你"},{3,"他"}});
std::list&ClassFoo&::
foo.sort(compare);
for (it = foo.begin(); it != foo.end(); it++ )
std::cout && it-&i && " " && it-&s && std::
int main()
ClassFoo::ListExample3();
}输出:一个有用的宏类似
标准中新增的初始化列表构造函数。#define CLASSFOO_LIST(type, name, ...) \
static const type name##_a[] = __VA_ARGS__; \
std::list&type& name(name##_a, name##_a + sizeof(name##_a) / sizeof(*name##_a))可以这样使用:CLASSFOO_LIST(int,foo,{1,2,3,4,5,6,7});posts - 431,&
comments - 344,&
trackbacks - 0
&Don't Repeat Yourself
座右铭:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
公告:本博客在此声明部分文章为转摘,只做资料收集使用。
微信: szhourui
QQ: Email:lsi.zhourui&#
2728293031134591011121314151617181920212223242526273031123456
留言簿(15)
随笔分类(1019)
文章分类(3)
文章档案(21)
bio & chem
积分与排名
阅读排行榜
以前在java里面要想去除list中的重复数据可以使用两种方式实现:
1. 循环list中的所有元素然后删除重复
&&&&public & static&&&List&&removeDuplicate(List&list)& {
&&&&& for &( int &i& = & 0 ;&i& & &list.size()& - & 1 ;&i ++ )& {
&&&&&&&&& for &( int &j& = &list.size()& - & 1 ;&j& & &i;&j -- )& {
&&&&&&&&&&&&& &if &(list.get(j).equals(list.get(i)))& {
&&&&&&&&&&&&&&&& &list.remove(j);
&&&&&&&&&&&&&&& }
&&&&&&&&&& &}
&&&&&&&& &}&
2. 通过HashSet踢除重复元素
&&&&public & static&&&List&&removeDuplicate(List&list)& {
&&&&&&& HashSet&h& = & new &HashSet(list);
&&&&&&&&list.clear();
&&&&&&& list.addAll(h);
在groovy中当然也可以使用上面的两种方法, 但groovy自己提供了unique方法来去除重复数据
&&&&def list = [1, 2, 3, 2, 4, 1, 5]
&&&&list.unique()& // [1, 2, 3, 4, 5]
阅读(2105)

我要回帖

更多关于 setuniquelist 的文章

 

随机推荐