V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jacklong
V2EX  ›  Python

mylist = list(set(mylist))

  •  
  •   jacklong ·
    jack-long · 2015-01-11 16:12:10 +08:00 · 3450 次点击
    这是一个创建于 3594 天前的主题,其中的信息可能已经有所发展或是发生改变。

    去除 List 中重复元素

    7 条回复    2015-01-14 23:45:19 +08:00
    9hills
        1
    9hills  
       2015-01-11 17:32:56 +08:00   ❤️ 1
    set下,list顺序就变了。你再list也没用

    保持顺序:
    a = set()
    l2 = [i for i in l1 if not (i in a or a.add(i))]
    dant
        2
    dant  
       2015-01-11 21:33:41 +08:00 via iPhone
    话说有没有 orderedset (
    yiding
        3
    yiding  
       2015-01-11 21:52:49 +08:00
    @dant 集合是三大特性之一,无序性
    zergling
        4
    zergling  
       2015-01-11 22:04:39 +08:00
    或者:
    from collections import OrderedDict
    l2 = OrderedDict(zip(l1, l1)).keys()
    deepurple
        5
    deepurple  
       2015-01-11 22:55:14 +08:00
    刚看那个董伟明的《Python高级编程》的视频里讲到了这个
    raquelken
        6
    raquelken  
       2015-01-12 11:53:09 +08:00
    @zergling 用dict的key去重必须是list元素是可以hashed的,而且还有这个方法 {}.fromkeys(mylist).keys()
    dongweiming
        7
    dongweiming  
       2015-01-14 23:45:19 +08:00   ❤️ 1
    @deepurple 我来填坑: 标准答案是这里 https://docs.python.org/2/faq/programming.html#id40

    以上都是一些trick

    In [1]: mylist = ['a', 'b', 'r', 'a', 'b', 'k', 'v', 'b']

    In [2]: %timeit list(set(mylist))
    The slowest run took 7.63 times longer than the fastest. This could mean that an intermediate result is being cached
    1000000 loops, best of 3: 781 ns per loop

    In [3]: %timeit {}.fromkeys(mylist).keys()
    The slowest run took 4.30 times longer than the fastest. This could mean that an intermediate result is being cached
    1000000 loops, best of 3: 887 ns per loop

    In [4]: from collections import OrderedDict

    In [5]: %timeit OrderedDict.fromkeys(mylist).keys()
    The slowest run took 11.18 times longer than the fastest. This could mean that an intermediate result is being cached
    100000 loops, best of 3: 18.5 µs per loop

    In [6]: %timeit list({}.fromkeys(mylist))
    The slowest run took 5.99 times longer than the fastest. This could mean that an intermediate result is being cached
    1000000 loops, best of 3: 995 ns per loop
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5715 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:16 · PVG 11:16 · LAX 19:16 · JFK 22:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.