滥用清单理解? [英] Misuse of list comprehensions?
问题描述
我昨晚发布了这个代码以回应另一个帖子,在我发布了它后,我想知道我是否滥用了列表理解。这两个例子是
:
示例1:
----------- ---------
def compress(s):
new = []
for c in s:
如果c不是新的:
new.append(c)
return''''。join(new)
----------------------
示例2:
------------------------
def compress(s):
new = [ ]
[如果c不在新的话,则新的支持(c)用于c的s
返回''''。join(新)
--------------------------
在例1中,打算进入-place变更是明确的,并且它被用作每个人都期望它被使用的
。然而,在示例2中,我开始认为这可能是对列表推导的滥用,因为我不会将结果分配给任何事物(我也不是)甚至以任何
的方式使用结果。)
大家对此有何看法?应该使用这个
方式列表推导,还是应该只用它们来实际创建一个新的列表,然后将其分配给变量/返回/等等?
I posted this code last night in response to another thread, and after I
posted it I got to wondering if I had misused the list comprehension. Here''s
the two examples:
Example 1:
--------------------
def compress(s):
new = []
for c in s:
if c not in new:
new.append(c)
return ''''.join(new)
----------------------
Example 2:
------------------------
def compress(s):
new = []
[new.append(c) for c in s if c not in new]
return ''''.join(new)
--------------------------
In example 1, the intention to make an in-place change is explicit, and it''s
being used as everyone expects it to be used. In example 2, however, I began
to think this might be an abuse of list comprehensions, because I''m not
assigning the result to anything (nor am I even using the result in any
way).
What does everyone think about this? Should list comprehensions be used this
way, or should they only be used to actually create a new list that will
then be assigned to a variable/returned/etc.?
推荐答案
John Salerno写道:
John Salerno wrote:
我昨晚发布此代码以回应另一个线程,在我发布了它后,我想知道我是否滥用了列表理解。
这里有两个例子:
示例1:
--------------------
def compress(s) :
new = []
for c in s:
如果c不是新的:
new.append(c)
返回''''。join(新)
---------------- ------
示例2:
--------------------- ---
def compress(s):
new = []
[new.append(c)for c in s if if c不是新的]
返回' ''。join(新)
--------------------------
在示例1中,进行就地更改的意图是明确的,并且
它被使用,因为每个人都希望它被使用。然而,在示例2中,我开始认为这可能是滥用列表推导,因为
我不会将结果分配给任何东西(我也不是)甚至以任何方式使用结果
)。
大家对此有何看法?应该以这种方式使用列表推导
,还是应该只用它们来实际创建一个新的列表,然后将
分配给变量/返回/等等?
I posted this code last night in response to another thread, and after I
posted it I got to wondering if I had misused the list comprehension.
Here''s the two examples:
Example 1:
--------------------
def compress(s):
new = []
for c in s:
if c not in new:
new.append(c)
return ''''.join(new)
----------------------
Example 2:
------------------------
def compress(s):
new = []
[new.append(c) for c in s if c not in new]
return ''''.join(new)
--------------------------
In example 1, the intention to make an in-place change is explicit, and
it''s being used as everyone expects it to be used. In example 2, however,
I began to think this might be an abuse of list comprehensions, because
I''m not assigning the result to anything (nor am I even using the result
in any way).
What does everyone think about this? Should list comprehensions be used
this way, or should they only be used to actually create a new list that
will then be assigned to a variable/returned/etc.?
上面的代码几乎是禁忌,因为它有二次运行时
的行为。
话虽如此,我自己也用这个成语。但是我没有看到任何错误
使用list-comp作为循环缩写。因为这是实际的
目的。而且,在非函数式语言中,如果不需要l值,则不总是分配
。这是
在语言中有副作用的结果 - 而python有它们。
Diez
the above code is pretty much of a no-no because it has quadratic runtime
behavior.
That being said, I use that idiom myself. But I don''t see anything wrong
with using a list-comp as loop-abbreviation. because that is it''s actual
purpose. And also it is common in non-functional languages that l-values
aren''t always assigned, if the aren''t needed. It''s the consequence of
having side-effects in a language - and python has them.
Diez
< br>
John Salerno:
John Salerno:
大家对此有何看法?
What does everyone think about this?
示例2构建一个列表,然后将其丢弃。这只是一个浪费时间(和时间)的浪费。
再见,
熊宝宝
The Example 2 builds a list, that is then thrown away. It''s just a
waste of memory (and time).
Bye,
bearophile
是************ @ lycos.com 写道:
be************@lycos.com wrote:
John Salerno:
John Salerno:
>大家都在想什么对这个?
>What does everyone think about this?
示例2构建一个列表,然后将其丢弃。这只是浪费内存(和时间)的浪费。
The Example 2 builds a list, that is then thrown away. It''s just a
waste of memory (and time).
不,它不是。它使用append,因为它在
if-expression中引用它自己。所以附加(c)是必要的 - 因此作业可能会因为基本上没用而可能是无效的。
Diez
No, it doesn''t. It uses append because it refers to itself in the
if-expression. So the append(c) is needed - and thus the assignment
possible but essentially useless.
Diez
>
这篇关于滥用清单理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!