The best way to get unique elements of a list in Python
2016-07-09 20:35
831 查看
The best way to get unique elements of a list in Python
Inspired by the Book Machine Learning in Action
最近在学习和实现 Machine Learning in Action 这本书的时候,在Chapter 3 看到了一个非常有趣的结论:从列表中创建集合是Python语言得到列表中唯一元素的最快方法。
也就是说,要从一个list中不重复地得到它所有的元素,最好的方法是用内置函数 set 来将这个 list 变为一个元组。
For example:
>>> l = list([‘a’,’b’,’c’,’a’,’a’,’c’,’b’,’b’])
>>> l
[‘a’, ‘b’, ‘c’, ‘a’, ‘a’, ‘c’, ‘b’, ‘b’]
>>> elements = set(l)
>>> elements
set([‘a’, ‘c’, ‘b’])
也是非常有意思!
相关文章推荐
- Python学习笔记之缩进
- 好用的Python IDLE Sublime Text 3推荐
- 使用lxml的xpath读取网页中的表格并转化为pandas的DataFrame
- NumPy的详细教程
- Python 元组 (教程 1)
- Python学习笔记8:Python条件和循环
- Python File(文件) 方法(教程5)
- Python 面向对象(教程4)
- Python 函数 (教程 2)
- Python 模块(教程3)
- 输入$ python text.py出错的原因——在windows系统使用idle的情况下
- python扩展实现方法--python与c混和编程
- 使用C写Python的模块
- python笔记4
- python系列笔记3
- tinypy源码笔记(二)——目录及构建分析
- 编写模块python
- Python实现在给定整数序列中找到和为100的所有数字组合
- windows下顺序安装numpy,matplotlib,scipy
- python系列2