python之list去重

问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变。

直观方法

最简单的思路就是:

复制代码代码如下:

ids = [1,2,3,3,4,2,3,4,5,6,1]
news_ids = []
for id in ids:
    if id not in news_ids:
        news_ids.append(id)

print news_ids

这样也可行,但是看起来不够爽。

用set

另外一个解决方案就是用set:

复制代码代码如下:

ids = [1,4,3,3,4,2,3,4,5,6,1]
ids = list(set(ids))

这样的结果是没有保持原来的顺序。

按照索引再次排序

最后通过这种方式解决:

复制代码代码如下:

ids = [1,4,3,3,4,2,3,4,5,6,1]
news_ids = list(set(ids))
news_ids.sort(ids.index)

使用itertools.grouby

文章一开始就提到itertools.grouby, 如果不考虑列表顺序的话可用这个:

复制代码代码如下:

ids = [1,4,3,3,4,2,3,4,5,6,1]
ids.sort()
it = itertools.groupby(ids)

for k, g in it:
    print k

关于itertools.groupby的原理可以看这里:http://docs.python.org/2/library/itertools.html#itertools.groupby

网友补充:用reduce

网友reatlk留言给了另外的解决方案。我补充并解释到这里:

复制代码代码如下:

In [5]: ids = [1,4,3,3,4,2,3,4,5,6,1]

In [6]: func = lambda x,y:x if y in x else x + [y]

In [7]: reduce(func, [[], ] + ids)
Out[7]: [1, 4, 3, 2, 5, 6]

上面是我在ipython中运行的代码,其中的 lambda x,y:x if y in x else x + [y] 等价于 lambda x,y: y in x and x or x+[y] 。

思路其实就是先把ids变为[[], 1,4,3,......] ,然后在利用reduce的特性。reduce解释参看这里:http://docs.python.org/2/library/functions.html#reduce

时间: 2024-08-03 18:02:22

python之list去重的相关文章

python pandas dataframe 去重函数

今天笔者想对pandas中的行进行去重操作,找了好久,才找打相关的函数 先看一个小例子 <span style="font-size:18px;">from pandas import Series, DataFrame data = DataFrame({'k': [1, 1, 2, 2]}) print data IsDuplicated = data.duplicated() print IsDuplicated print type(IsDuplicated) da

【python】数组去重

直接用set就行,比如: l = [1, 1, 2, 2, 3, 4, 5] s = set(l) c = [i for i in s] print c 结果为: [1, 2, 3, 4, 5] 其中第三行利用了python的列表生成式

python根据uuid去重,获取请求重各种动作的次数

#!/usr/bin/python # -*- coding: utf-8 -*- #RUN     // 程序启动 #EXIT    // 程序退出 #START   // 热点启动 #STOP    // 热点停止 #ONL     // 客户端上线 #OFFL    // 客户端下线 #INSTALL //安装 #UNINSTALL //卸载 import re #模式匹配UUID patternUid=re.compile(r'(\w){8}-(\w){4}-(\w){4}-(\w){4

Python对list去重的各种方法

参考原文:https://www.the5fire.com/python-remove-duplicates-in-list.html 需求:去list进行去重,去重后保证顺序不变 方法1:for循环 ids = [1, 2, 3, 3, 4, 2, 3, 4, 5, 6, 1] new_ids = [] for id in ids: if id not in new_ids: new_ids.append(id) print("new_ids==>", new_ids) 方法2

Python嵌套列表去重

raw_list = [ [ 'CS_SUPP_INFO', 'A', '1'], [ 'CS_SUPP_INFO', '1', 'A'], [ 'CS_SUPP_INFO', '1', 'A'], [ 'CS_SUPP_INFO', 'A', '1'], [ 'CS_SUPP_INFO', 'A', '2'], [ 'CS_SUPP_INFO', 'A', '2'], [ 'CS_SUPP_INFO', 'A', '3'] ] # 排序,只有排序后,下面用集合去重才能排除子列表中子元素顺序的影

python 实现对象去重

利用set()方法实现对象去重,重写__hash__方法和__eq__方法告诉程序什么样的对象是同一个对象 # 写一个类 拥有100个对象 # 拥有三个属性 name age sex # 如果两个对象的name 和 sex 完全相同 # 我们就认为这是一个对象 # 忽略age属性 做这100个对象的去重工作 class Person(): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex

Python 列表元素去重的3种方法

以前面试的时候遇到过这个问题,今天闲着整理了以下,大概想到以下三种方法. <span style="font-size:18px;">class delect_duplicate: def method_set(self,mlist): print("method_set is called") print("before process mlist is", mlist) l2 = set(mlist) print("af

python 对列表去重,并保持列表原来顺序

mailto = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] addr_to = list(set(mailto)) addr_to.sort(key = mailto.index)

python列表去重 冒泡排序 插序排序

python对列表去重例子 #!/usr/bin/env python arr_num1 = [1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4] arr_num2 = [2,1,3,2,43,234,454,452,234,14,21,14] num_list = [] for i in arr_num1:     if i in arr_num2 and i not in num_list:      num_list.appen