首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: del in for循环

Python: del in for循环
EN

Stack Overflow用户
提问于 2015-04-06 18:58:09
回答 4查看 2K关注 0票数 2

问题:

代码语言:javascript
复制
test1 = [1, 2, 3]
for i in range(len(test1)): #Want to do the below for every element in test1 
    print(test1[i])
    if test1[i] % 2 == 0: #if the element meets certain conditions, it gets deleted
        del test1[i] 
        #The thing in test1 gets deleted, but the range gets not updated,
        #and i iterates nonetheless, so 1 element gets ignored,
        #and in the end, I get "list index out of range"

(在实际代码中,有更多的if语句,数组中的内容不是整数,而是对象?!一门课,所以清单理解更多.)

我想出了一个方法:

代码语言:javascript
复制
test1 = [1, 2, 3]
i = 0
x = len(test1)
while i < x:
    print(test1[i])
    if test1[i] % 2 == 0:
        del test1[i]
        x -= 1
        i -= 1
    i += 1

但正如你所看到的,这是5行长(对于每一个模型,我必须增加2行.),丑陋,烦人。有什么更好的方法来做这样的事吗?比如,一个重做命令,它直接进入for循环,更新范围,让您选择i或其他什么?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-04-06 19:03:02

你试过在靶场上使用reversed吗?

代码语言:javascript
复制
test1 = [1, 2, 3]
for i in reversed(range(len(test1))):
    print(test1[i])
    if test1[i] % 2 == 0:
        del test1[i]
>>> 3
>>> 2
>>> 1

print(test1)
>>> [1, 3]

这样,当您删除一个元素时,数组就会减少,但是由于您正在以相反的顺序遍历列表,因此只有已经处理过的元素的索引才会受到影响。

编辑

在阅读了所有的评论后,我决定运行一些小基准。我创建了一个包含1,000个元素的列表和一个包含10,000,000个元素的列表,并尝试了以下三种方法:

  • #1从列表中删除
  • #2创建一个具有列表理解能力的新列表
  • #3创建一个具有经典格式的新列表

对于包含1,000个元素的列表,时间并不重要,但对于10,000,000个元素列表而言,从列表中删除几乎是不可能的(几个小时比半秒时间创建一个新列表)。

基于1,000元测试

代码语言:javascript
复制
>>> Test 0, 11th element: 7e-06 seconds
>>> Test 0, second last element: 2e-06 seconds
>>> Test 1: 0.00017 seconds
>>> Test 2: 0.000103 seconds
>>> Test 3: 0.000234 seconds

10,000,000元素测试

代码语言:javascript
复制
>>> Test 0, 11th element: 0.011888 seconds
>>> Test 0, second last element: 4e-06 seconds
>>> Test 1: Too long!!
>>> Test 2: 0.941158 seconds
>>> Test 3: 0.681262 seconds

在这方面,我强烈建议创建一个具有列表理解性或常规for循环的新列表。

这里是我的代码:

代码语言:javascript
复制
from datetime import datetime
from random import randint


# Create my test lists of 10 000 000 elements
test_0 = []
#nb_el = 10000000
nb_el = 1000
while (len(test_0) < nb_el):
    test_0.append(randint(0, 100))
test_1 = test_0[:] # clone list1
test_2 = test_0[:] # clone list1
test_3 = test_0[:] # clone list1


# Test #0
# Remove the 11th element and second last element
d1 = datetime.now()
del test_0[10]
d2 = datetime.now()
print('Test 0, 11th element: ' +
    str((d2-d1).microseconds / 1000000.0) + ' seconds')

d1 = datetime.now()
del test_0[nb_el-2]
d2 = datetime.now()
print('Test 0, second last element: '
    + str((d2-d1).microseconds / 1000000.0) + ' seconds')


# Test #1 | TOO SLOW TO BE RAN with 10 000 000 elements
# Delete every element where element is a multiple of 2
d1 = datetime.now()
for i in reversed(range(len(test_1))):
    if test_1[i] % 2 == 0:
        del test_1[i]
d2 = datetime.now()
print('Test 1: ' + str((d2-d1).microseconds / 1000000.0) + ' seconds')

# Test #2
# Create a new list with every element multiple of 2
# using list comprehension
d1 = datetime.now()
test_2_new = [x for x in test_2 if x % 2 == 0]
d2 = datetime.now()
print('Test 2: ' + str((d2-d1).microseconds / 1000000.0) + ' seconds')

# Test #3
# Create a new list with every element multiple of 2
# using for loop
d1 = datetime.now()
test_3_new = []
for i in range(len(test_3)):
    if test_3[i] % 2 == 0:
        test_3_new.append(test_3[i])
d2 = datetime.now()
print('Test 3: ' + str((d2-d1).microseconds / 1000000.0) + ' seconds')
票数 3
EN

Stack Overflow用户

发布于 2015-04-06 19:04:02

使用列表理解

与…有关的东西:

代码语言:javascript
复制
list = [x for x in list if x % 2 == 1] # Keep anything that is not even
for thing in list:
    print(thing)

这将创建一个包含与条件匹配的任何内容的新列表。然后打印新列表

代码语言:javascript
复制
>>> test1 = [1,2,3]
>>> list = [x for x in test1 if x % 2 is 1]
>>> print(list)
[1, 3]
票数 6
EN

Stack Overflow用户

发布于 2015-04-06 19:04:43

假设您希望在从列表中删除每个数字之前打印它们。

代码语言:javascript
复制
myList = [1,2,3]

for n in test1:
    print n

test1 = [n for n in myList if n % 2] # 0 = False by default in python. No need for == 0 / > 0.

如果您非常希望使用您编写的代码,那么您可能更容易理解:

代码语言:javascript
复制
test1 = [1, 2, 3]
for i in range(len(test1)): #Want to do the below for every element in test1 
    print(test1[i])


    # Either check if the index is not greather than the length of the list.

    if test1[i] % 2 == 0 and i <= len(test1): #if the element meets certain conditions, it gets deleted

    # Or use try, except (Will be slow if your list is great in size as there will be many exceptions caught.

    if test1[i] % 2 == 0:
        try:
            del test1[i] 
        except:
            continue
        #The thing in test1 gets deleted, but the range gets not updated,
        #and i iterates nonetheless, so 1 element gets ignored,
        #and in the end, I get "list index out of range"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29477706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档