从列表中删除项目
嘿,我试图从列表中删除一个项目(不使用 set
):
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
for k in range(1,len(list1) - 1):
if (list1[k] == list1[k - 1]):
list1.remove(list1[k])
print "length = " + str(len(list1))
set
函数工作正常,但我想应用此方法。除非我得到:
IndexError: list index out of range
在声明中:
if (list1[k] == list1[k - 1]):
编辑添加 (感谢 Ned Batchelder)工作代码是:
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
k = 0
while k < len(list1) - 1: # while loop instead of for loop because "The range function is evaluated once before the loop is entered"
k += 1
if (list1[k] == list1[k - 1]):
list1.remove(list1[k])
list1.sort()
k -= 1 # "If you find a duplicate, you don't want to move onto the next iteration, since you'll miss potential runs of more than two duplicates"
print "length = " + str(len(list1))
Hey, I was trying to delete an item form a list (without using set
):
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
for k in range(1,len(list1) - 1):
if (list1[k] == list1[k - 1]):
list1.remove(list1[k])
print "length = " + str(len(list1))
The set
function works fine, but i want to apply this method. Except I get:
IndexError: list index out of range
on the statement:
if (list1[k] == list1[k - 1]):
Edited to add
(Thanks to Ned Batchelder) the working code is:
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
k = 0
while k < len(list1) - 1: # while loop instead of for loop because "The range function is evaluated once before the loop is entered"
k += 1
if (list1[k] == list1[k - 1]):
list1.remove(list1[k])
list1.sort()
k -= 1 # "If you find a duplicate, you don't want to move onto the next iteration, since you'll miss potential runs of more than two duplicates"
print "length = " + str(len(list1))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码不起作用,因为在循环中,您正在迭代原始列表中的所有索引,但会逐渐缩短列表。在迭代结束时,您将访问不再存在的索引:
在进入循环之前,
range
函数会计算一次,创建列表中所有索引的列表。每次调用remove
都会将列表缩短一,因此,如果您删除任何元素,您一定会在列表末尾收到错误。如果您想使用这样的循环,请尝试:
我修复了其他一些问题:
Your code doesn't work because in your loop, you are iterating over all the indexes in the original list, but shortening the list as you go. At the end of the iteration, you will be accessing indexes that no longer exist:
The
range
function is evaluated once before the loop is entered, creating a list of all the indexes in the list. Each call toremove
shortens the list by one, so if you remove any elements, you're guaranteed to get your error at the end of the list.If you want to use a loop like this, try:
I fixed a few other things:
看起来好像您正在尝试统一列表(澄清会很棒),所以请看这里: http://www.peterbe.com/plog/uniqifiers-benchmark
这里也有这个问题:在 Python 中,从列表中删除重复项以使所有元素都是唯一的最快算法是什么 *while保持秩序*?
It looks as if you're trying to uniquify a list (clarification would be awesome) so take a look here: http://www.peterbe.com/plog/uniqifiers-benchmark
There is also this question here on SO: In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique *while preserving order*?
不要删除项目,而是在新列表中编写您想要的内容的列表理解:
您的方法会中断,因为您从列表中删除了项目。当您这样做时,列表会变得更短,并且下一个循环迭代会跳过一个项目。假设您查看 k=0 且 L = [1,2,3]。您删除第一项,因此 L = [2,3] 且下一个 k=1。所以你看看 L[1],它是 3——你跳过了 2!
所以:永远不要改变你迭代的列表
Instead of removing items Write a list comprehension of the things you want in the new list:
Your method breaks because you remove items from the list. When you do that, the list becomes shorter and the next loop iteration has skipped a item. Say you look at k=0 and L = [1,2,3]. You delete the first item, so L = [2,3] and the next k=1. So you look at L[1] which is 3 -- you skipped the 2!
So: Never change the list you iterate on
您可以使用
del
:You can use
del
: