"典型"从列表中删除元素的方法 [英] "Canonical" way of deleting elements from lists

查看:70
本文介绍了"典型"从列表中删除元素的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


从字符串列表中我想删除所有空字符串。这有效:


关键字中的'''':keywords.remove('''')


但是,长期以来术语C程序员看起来像是一个尴尬的方式来实现一个简单的目标,因为列表必须在每次迭代中重新评估

。有没有办法只是走一次列表然后扔掉

不需要的元素?


当这些小东西真实时,我开始编程性能

问题,所以当看起来好像没什么问题时我会有某种畏缩反射。


罗伯特

Hello,

From a list of strings I want to delete all empty ones. This works:

while '''' in keywords: keywords.remove('''')

However, to a long-term C programmer this looks like an awkward way of
accomplishing a simple goal, because the list will have to be re-evaluated
in each iteration. Is there a way to just walk the list once and throw out
unwanted elements as one goes along?

I started programming back when such little things were real performance
issues, so I have some sort of cringe reflex when something looks
inefficient.

robert

推荐答案

Robert Latest写道:
Robert Latest wrote:

>>来自a我要删除所有空字符串的字符串列表。这适用:
>>From a list of strings I want to delete all empty ones. This works:



关键字中的'''':keywords.remove('''')


但是,对于一个长期的C程序员来说,这看起来像是一个尴尬的方式来完成一个简单的目标,因为每次迭代都需要重新评估列表



while '''' in keywords: keywords.remove('''')

However, to a long-term C programmer this looks like an awkward way of
accomplishing a simple goal, because the list will have to be re-evaluated
in each iteration.



你正在使用二次算法(in是线性搜索,并删除

必须移动一切每次调用),你担心Python获取变量需要花费多少时间?

you''re using a quadratic algorihm ("in" is a linear search, and remove
has to move everything around for each call), and you''re worried about
the time it takes Python to fetch a variable?


有没有办法只需走一次列表就可以随意丢弃不需要的

元素?
Is there a way to just walk the list once and throw out unwanted
elements as one goes along?



创建一个新列表总是几乎正确的方式来做这样的事情,比如

。在这种特殊情况下,filter()或列表推导是好的

选择:


keywords = filter(无,关键字)#get" true"仅限商品


keywords = [k为关键字中的k如果k]


也可以看到:

http://effbot.org/zone/python-list.htm#modifying


< / F>

creating a new list is always almost the right way to do things like
this. in this specific case, filter() or list comprehensions are good
choices:

keywords = filter(None, keywords) # get "true" items only

keywords = [k for k in keywords if k]

also see:

http://effbot.org/zone/python-list.htm#modifying

</F>


Fredrik Lundh写道:
Fredrik Lundh wrote:

创建一个新列表总是几乎正确的方式来执行诸如
creating a new list is always almost the right way to do things like



message = message.replace(" always almost) ,几乎总是)

message = message.replace("always almost", "almost always")


Robert最新< bo ******* @ yahoo.comwrites:
Robert Latest <bo*******@yahoo.comwrites:

从字符串列表中我想删除所有空字符串。这有效:


而关键字中的''''关键字:keywords.remove('''')
From a list of strings I want to delete all empty ones. This works:

while '''' in keywords: keywords.remove('''')



如果你'正在寻找一个快速(没有二次行为)和方便的

的方式,你可以这样做:


keywords = [s对于s中的关键字如果s!='''''


但这会创建一个新列表,对于长列表可能不需要

空元素(或共享列表)。它还迭代了Python循环中每个列表元素的

,这可能需要一些时间才能获得很长的

列表。

If you''re looking for a quick (no quadratic behavior) and convenient
way to do it, you can do it like this:

keywords = [s for s in keywords if s != '''']

But that creates a new list, which might not be wanted for long lists
with few empty elements (or for shared lists). It also iterates over
every list element in a Python loop, which can take some time for long
lists.


有没有办法只需走一次列表就扔出不需要的

元素?
Is there a way to just walk the list once and throw out unwanted
elements as one goes along?



我会这样做:


i = 0

而1:

尝试:

i = keywords.index('''',i)

除了ValueError:

break

del keywords [i]


甚至:


试试:

i = 0

而1:

i = keywords.index('''',i)#throws ValueError并停止循环

del keywords [i]

除了ValueError:

pass


在这两种情况下,搜索空字符串都是在高效的C

代码,你只在实际匹配中循环Python。

I''d do it like this:

i = 0
while 1:
try:
i = keywords.index('''', i)
except ValueError:
break
del keywords[i]

Or even:

try:
i = 0
while 1:
i = keywords.index('''', i) # throws ValueError and stops the loop
del keywords[i]
except ValueError:
pass

In both cases the search for the empty string is done in efficient C
code, and you only loop in Python over the actual matches.


这篇关于&QUOT;典型&QUOT;从列表中删除元素的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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