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

2015-06-28 01:10:17 +08:00
 plantparknet
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] 这段代码展开后是什么?
3166 次点击
所在节点    Python
16 条回复
ca1n
2015-06-28 01:18:24 +08:00
。。。去重复
xupefei
2015-06-28 01:18:38 +08:00
>foreach x in mList
> if(!cleanlist.contains(x))
> cleanlist.add(x)

就这样喽。
xupefei
2015-06-28 01:20:21 +08:00
@xupefei 我还特意在行首加了个 > 防止空格被去掉来着,可还是被搞掉了。
帖子内容中的空格能不能转义一下? @Livid
yzyzsun
2015-06-28 01:27:40 +08:00
顺便问下 Python 有类似于 array.uniq! 的方法吗?
plantparknet
2015-06-28 01:31:25 +08:00
@xupefei 没看明白,首先cleanlist为空,如何实现查询myList中的重复呢?
plantparknet
2015-06-28 01:34:38 +08:00
@ca1n 知道这段代码实现的是去重,但不理解为何如此写,x应该为myList中的数值,如何实现查询重复的
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
2015-06-28 01:54:48 +08:00
@xupefei 十分感谢~~ 这么详细~~ 感激涕流啊~~
gaotongfei
2015-06-28 02:57:11 +08:00
@yzyzsun set
orzfly
2015-06-28 03:17:12 +08:00
@yzyzsun list(set([1,2,3,4,4,4,4,4,4,4,4,4]))
rrkelee
2015-06-28 07:39:09 +08:00
staticor
2015-06-28 09:06:25 +08:00
@yzyzsun 或者用其它第三方的 numpy\pandas .unique()替代 ruby 里 uniq
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
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
2015-06-28 12:35:19 +08:00
@cc7756789 列表推导式 还不是翻译过来的 主要是便于理解嘛 http://encyclopedia.thefreedictionary.com/List+comprehension#History
kaneg
2015-07-01 11:48:15 +08:00
@orzfly 用set的一个副作用是顺序可能会变化

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/201659

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX