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
plantparknet
V2EX  ›  Python

[cleanlist.append(x) for x in myList if x not in cleanlist] 这段代码展开后是什么?

  •  
  •   plantparknet · 2015-06-28 01:10:17 +08:00 · 3157 次点击
    这是一个创建于 3219 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://stackoverflow.com/questions/7961363/python-removing-duplicates-in-lists

    myList = [1, 2, 3, 1, 2, 5, 6, 7, 8]
    cleanlist = []
    [cleanlist.append(x) for x in myList if x not in cleanlist]



    [cleanlist.append(x) for x in myList if x not in cleanlist] 这段代码展开后是什么?
    16 条回复    2015-07-01 11:48:15 +08:00
    ca1n
        1
    ca1n  
       2015-06-28 01:18:24 +08:00
    。。。去重复
    xupefei
        2
    xupefei  
       2015-06-28 01:18:38 +08:00
    >foreach x in mList
    > if(!cleanlist.contains(x))
    > cleanlist.add(x)

    就这样喽。
    xupefei
        3
    xupefei  
       2015-06-28 01:20:21 +08:00
    @xupefei 我还特意在行首加了个 > 防止空格被去掉来着,可还是被搞掉了。
    帖子内容中的空格能不能转义一下? @Livid
    yzyzsun
        4
    yzyzsun  
       2015-06-28 01:27:40 +08:00 via iPhone
    顺便问下 Python 有类似于 array.uniq! 的方法吗?
    plantparknet
        5
    plantparknet  
    OP
       2015-06-28 01:31:25 +08:00
    @xupefei 没看明白,首先cleanlist为空,如何实现查询myList中的重复呢?
    plantparknet
        6
    plantparknet  
    OP
       2015-06-28 01:34:38 +08:00
    @ca1n 知道这段代码实现的是去重,但不理解为何如此写,x应该为myList中的数值,如何实现查询重复的
    xupefei
        7
    xupefei  
       2015-06-28 01:46:02 +08:00
    @plantparknet
    第一步: cleanlist=[],x=1,cleanlist中包含x吗,不包含,把x添加到cleanlist中
    第二步,cleanlist=[1],x=2,cleanlist中包含x吗,不包含,把x添加到cleanlist中
    第三步,cleanlist=[1, 2],x=3,cleanlist中包含x吗,不包含,把x添加到cleanlist中
    第四步,cleanlist=[1, 2, 3],x=1,cleanlist中包含x吗,包含,什么也不做
    第五步,cleanlist=[1, 2, 3],x=2,cleanlist中包含x吗,包含,什么也不做
    第六步,cleanlist=[1, 2, 3],x=5,cleanlist中包含x吗,不包含,把x添加到cleanlist中
    第七步,cleanlist=[1, 2, 3, 5],……………………………………
    plantparknet
        8
    plantparknet  
    OP
       2015-06-28 01:54:48 +08:00
    @xupefei 十分感谢~~ 这么详细~~ 感激涕流啊~~
    gaotongfei
        9
    gaotongfei  
       2015-06-28 02:57:11 +08:00 via iPhone
    @yzyzsun set
    orzfly
        10
    orzfly  
       2015-06-28 03:17:12 +08:00
    @yzyzsun list(set([1,2,3,4,4,4,4,4,4,4,4,4]))
    staticor
        12
    staticor  
       2015-06-28 09:06:25 +08:00
    @yzyzsun 或者用其它第三方的 numpy\pandas .unique()替代 ruby 里 uniq
    cc7756789
        13
    cc7756789  
       2015-06-28 10:19:46 +08:00
    这是列表生成式( List Comprehension ),用于简化多行 if 结构的语句,生产一个list,然后去掉重复的元素,把他写成常规语句就是:

    ```python
    mylist = [1,1,2,2,3,4]
    tolist = []
    for n in mylist:
    if n not in tolist:
    tolist.append(n)
    ```

    还有一个可用 set 去重复元素的更简单方法。

    ```python
    >>> a = set([1,1,2,2,3,4])
    >>> a
    set([1, 2, 3, 4])
    >>> list(a)
    [1, 2, 3, 4]
    ```

    Python还有一个生成器(Generator),其就等于函数使用yield进行return,只需要把 `[]` 括号变成 `()` 括号:

    ```python
    >>> a = (x for x in range(10))
    >>> a
    <generator object <genexpr> at 0x7f7c1ebfd050>
    >>> for x in a:
    ... print x
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>>

    #和下面相同
    >>> def a():
    ... for x in range(10):
    ... yield x
    >>> a()
    <generator object a at 0x7f7c1ebfde10>
    >>> for x in a():
    ... print x
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    ```


    这样不会立即生成list,而是生成一个generator对象,需要用的时候再进行取值,可以节省大量内存。
    你也可以利用 generator 拥有的 `next()` 方法取值。(注意:如果你取出,值就被弹出不存在了,除非你再次启动Python解释器)

    ```python
    >>> a = (x for x in range(10))
    >>> a.next()
    0
    >>> a.next()
    1
    >>> a.next()
    2

    ```


    附:一直不知道为什么要叫List Comprehension,Comprehension意为 **理解;包含**,知道的也可以告诉我原因啊。
    cc7756789
        14
    cc7756789  
       2015-06-28 10:28:21 +08:00
    @cc7756789 补充:还有一种去重复的方法:

    >>> {}.fromkeys([1,1,1,1,3])
    {1: None, 3: None}
    >>> {}.fromkeys([1,1,1,1,3]).keys()
    [1, 3]
    aec4d
        15
    aec4d  
       2015-06-28 12:35:19 +08:00
    @cc7756789 列表推导式 还不是翻译过来的 主要是便于理解嘛 http://encyclopedia.thefreedictionary.com/List+comprehension#History
    kaneg
        16
    kaneg  
       2015-07-01 11:48:15 +08:00
    @orzfly 用set的一个副作用是顺序可能会变化
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1199 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:11 · PVG 02:11 · LAX 11:11 · JFK 14:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.