python列表语法,python列表循环技术语法

I am reading an article about python removing duplicate element in a list.

there is a function defined as:

def f8(seq): # Dave Kirby

# Order preserving

seen = set()

return [x for x in seq if x not in seen and not seen.add(x)]

However, i don't really understand the syntax for

[x for x in seq if x not in seen and not seen.add(x)]

what is this syntax ? how do I read it?

thank you.

解决方案

Firstly list comprehensions are usually easy to read, here is a simple example:

[x for x in seq if x != 2]

translates to:

result = []

for x in seq:

if x != 2:

result.append(x)

The reason why you can't read this code is because it is not readable and hacky code as I stated in this question:

def f8(seq):

seen = set()

return [x for x in seq if x not in seen and not seen.add(x)]

translates to:

def f8(seq):

seen = set()

result = []

for x in seq:

if x not in seen and not seen.add(x): # not seen.add(...) always True

result.append(x)

and relies on the fact that set.add is an in-place method that always returns None so not None evaluates to True.

>>> s = set()

>>> y = s.add(1) # methods usually return None

>>> print s, y

set([1]) None

The reason why the code has been written this way is to sneakily take advantage of Python's list comprehension speed optimizations.

Python methods will usually return None if they modify the data structure (pop is one of the exceptions)

I also noted that the current accepted way of doing this (2.7+) which is more readable and doesn't utilize a hack is as follows:

>>> from collections import OrderedDict

>>> items = [1, 2, 0, 1, 3, 2]

>>> list(OrderedDict.fromkeys(items))

[1, 2, 0, 3]

Dictionary keys must be unique, therefore the duplicates are filtered out.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值