list.clear()缺少?!? [英] list.clear() missing?!?

查看:70
本文介绍了list.clear()缺少?!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天试图清除一个列表(我很少这样做,考虑到

只是做l = []大多数情况下工作)并且感到震惊,震惊

注意到没有clear()方法。 Dicts有它,套装有它,

为什么列表必须是二等公民?

解决方案

Ville Vainio写道:

我今天试图清除一个清单(我很少这样做,考虑到大多数情况下只做l = [])感到震惊,感到震惊,注意到没有clear()方法。 Dicts有它,套装有它,
为什么列表必须是二等公民?




因为Python已经有一个完全有效的清除列表的方法,

也许吧?


del l [:]


(列表不是映射,所以鸭子打字争论不是真的

适用于此。)


< / F>

Fredrik Lundh写道:

我今天试图清除一个清单(我很少这样做,考虑到这只是做l = []在大部分时间都有效)并且震惊,震惊
注意到没有clear()方法。 Dicts有它,套装有它,
为什么列表必须是二等公民?
因为Python已经有一个完全有效的方法来清除列表,
也许?

del l [:]




好​​的。这是非常不明显的,但现在我已经看到它了,我可能会记住它。我做了一个愚蠢的而l:l.pop()循环自己。

(列表不是映射,所以鸭子打字参数不是真的适用于这里。)




我认为列表是一个可变集合,而clear()是

对他们来说当然是一种非常自然的操作。

Ville Vainio写道:

我今天试图清除一个列表(我很少这样做,考虑到
只是做l = []大多数情况下工作)并且是震惊,震惊
注意到没有clear()方法。 Dicts有它,套装有它,
为什么列表必须是二等公民?




这一直被提起(搜索档案你的

最喜欢的),但你的选择基本上(将你的名单重命名为lst for

可读性)之一::


del lst [:]


lst [:] = []


或者如果您不需要修改列表,


lst = []


就个人而言,我倾向于选择弗雷德里克的路线并使用第一个路线。


如果你对此感到非常强烈,你可能会考虑写一个PEP来获得
。这是有争议的,没有太多的机会获得改变。


STeVe


I tried to clear a list today (which I do rather rarely, considering
that just doing l = [] works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?

解决方案

Ville Vainio wrote:

I tried to clear a list today (which I do rather rarely, considering
that just doing l = [] works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?



because Python already has a perfectly valid way to clear a list,
perhaps ?

del l[:]

(lists are not mappings, so the duck typing argument don''t really
apply here.)

</F>


Fredrik Lundh wrote:

I tried to clear a list today (which I do rather rarely, considering
that just doing l = [] works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?
because Python already has a perfectly valid way to clear a list,
perhaps ?

del l[:]



Ok. That''s pretty non-obvious but now that I''ve seen it I''ll probably
remember it. I did a stupid "while l: l.pop()" loop myself.
(lists are not mappings, so the duck typing argument don''t really
apply here.)



I was thinking of list as a "mutable collection", and clear() is
certainly a very natural operation for them.


Ville Vainio wrote:

I tried to clear a list today (which I do rather rarely, considering
that just doing l = [] works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?



This gets brought up all the time (search the archives for your
favorite), but your options are basically (renaming your list to lst for
readability) one of::

del lst[:]

lst[:] = []

or if you don''t need to modify the list in place,

lst = []

Personally, I tend to go Fredrik''s route and use the first.

If you feel really strongly about this though, you might consider
writing up a PEP. It''s been contentious enough that there''s not much
chance of getting a change without one.

STeVe


这篇关于list.clear()缺少?!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆