List去重
来源:互联网 发布:软件开发交税 编辑:程序博客网 时间:2024/04/28 06:57
最近任务上有去重,随手翻了下发现有不少好方法啊~
记录一下,转自http://yxmhero1989.blog.163.com/blog/static/112157956201381443244790/:
一.{}.fromkeys(list).keys()
二.setlist2 ={}.fromkeys(list1).keys()
三.itertools.groubylist2 = list(set(list1))
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
四,笨方法
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
这四种都有个特点,去重后元素排序变了,效率 据说第一种比第二种快一点
五.索引再次排序 这种可以去重并且保持元素顺序
#要结果是[1, 4, 3, 2, 5, 6] 不要[1, 2, 3, 4, 5, 6]ids = [1,4,3,3,4,2,3,4,5,6,1]news_ids = list(set(ids))news_ids.sort(key=ids.index)print news_ids #[1, 4, 3, 2, 5, 6]
六:Reduce
ids = [1,4,3,3,4,2,3,4,5,6,1]func = lambda x,y:x if y in x else x + [y]print reduce(func, [[], ] + ids)#[1, 4, 3, 2, 5, 6]
参考:
http://the5fire.com/python-remove-duplicates-in-list.html
http://xcw31.diandian.com/post/2012-11-28/40042801718
http://www.benben.cc/blog/?p=386
http://blog.csdn.net/zhengnz/article/details/6265282
0 0
- python list去重
- List去重
- C# List去重
- list集合去重
- list 去重
- List去重
- List<对象>去重
- list去重排序
- python list 去重
- List<对象>去重
- Python list去重
- List集合去重
- List去重
- List排序去重
- python list 去重
- java list 去重
- list列表去重
- List集合去重
- Android targetSdkVersion引起的问题
- 蔡力能心得3
- onWindowFocusChanged重要作用 and Activity生命周期
- jsp中request对象获取客户端IP地址
- Hibernate 使用session 进行事务处理
- List去重
- jquery树状控件
- liunx不能进入图形界面问题一则
- 艾粽飘香,空相古寺钟声扬
- VC++编译lua库 无法解析lua符号问题
- C3P0连接池详细配置
- RTMP协议介绍与勘误
- bootstrap初识
- Eclipse中Velocity插件安装