[英]Remove elements from a list that occur in another list and return their indices
[英]Remove elements from a list, using another list as indices
我有一个列表primeList
和另一个列表ls
。 primeList是一个充满整数的列表,我需要从ls
删除具有在primeList
的索引的值。
例如,如果primelist = [1, 3 , 5]
和ls = [1, 2, 3, 4, 5, 6, 7]
,则应从ls
删除索引1、3和5,使ls = [1, 3, 5, 7]
目前,我正在尝试使用以下代码:
primeList = list(getPrimes())
ls = list(ls)
for i in primeList:
del ls[i]
return ls
这给了我以下错误:
Traceback (most recent call last):
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 26, in <module>
otherList = delPrimes(myList)
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 18, in delPrimes
del ls[i]
IndexError: list assignment index out of range`
我相信这是因为getPrimes的列表比ls大,但是我不确定如何在Python中解决此问题?
编辑-这是我当前的全部代码:
def delPrimes(*ls):
def getPrimes():
L = []
for x in range(2, 230):
isPrime = True
for y in range(2, x):
if x % y == 0:
isPrime = False
if isPrime:
L.append(x)
return L
primeList = list(getPrimes())
ls = list(ls)
for i in primeList:
del ls[i]
return ls
myList = list(range(1, 51))
print(myList)
print("--" * 40)
otherList = delPrimes(myList)
print(otherList)
作为一些功课的一部分,我们需要“在Python中编写一种方法,以删除列表中主要索引位置(最高索引位置50)处的项目。例如,它将删除索引位置2、3、5、7, …“我也相信我们必须使用'del'进行删除。
编辑2:
for i in reversed(primeList):
if i <= len(ls):
del ls[i]
else:
continue
return ls
使用列表理解来避免更改列表:
return [v for i, v in enumerate(ls) if i not in primeList]
您正在从列表的开头一开始删除元素,因此其他元素每个都向上移动一位。 第一次删除后,其余索引将一一分开,然后二一分开,依此类推:
>>> ls = [1, 2, 3, 4, 5, 6, 7]
>>> del ls[1]
>>> ls
[1, 3, 4, 5, 6, 7]
>>> ls[3]
5
>>> del ls[3]
>>> ls
[1, 3, 4, 6, 7]
>>> ls[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
您可以就地更改ls
,但随后需要反向处理索引,以便仅删除尚未移动的索引:
for index_to_remove in reversed(primeList):
del ls[index_to_remove]
但由于您已经在制作副本,因此在这里没有必要。
我看到Martijn在我之前给出了完整的答案。 他的回答是正确的,所以我投票给他。 如果有不清楚的地方,请问。 对您的代码的注释。
您的代码粘贴不良或不正确。 您在函数外部有一个return语句。 除此之外,我将尝试猜测由于代码被破坏而导致的意图。
如果在其中使用*
,则表示执行的是拆包值。 我认为这对于您所需要的不是必需的。
>>> def test(*ls):
print ls
>>> test("a", "b", "c")
('a', 'b', 'c')
>>> ls = [1,2,3]
>>> test(ls)
([1, 2, 3],)
>>> def test(ls):
print ls
>>> test("a", "b", "c")
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
test("a", "b", "c")
TypeError: test() takes exactly 1 argument (3 given)
我不确定您是否真的打算这样做。
您也可以重写getPrimes
函数,这样您就不需要花费时间来计算所有这些质数(当然,这是一个很小的数字,但是由于我已经给出了“技巧”,为什么不这样做)?
def getPrimes(top):
L = []
for x in range(2, top+1):
isPrime = True
for y in range(2, x):
if x % y == 0:
isPrime = False
if isPrime:
L.append(x)
return L
现在您可以像下面这样调用该函数:
>>> getPrimes(7)
[2, 3, 5, 7]
即使您的列表并不总是相同,这也很有用,因为您始终可以通过询问来调用函数。
>>> getPrimes(max(ls))
[2, 3, 5, 7]
max(ls)
在其中很明显,并返回列表中最大的元素(您拥有的最高质数)。 之后,只需遵循Martijns上有关如何删除元素的指示即可(颠倒顺序)。
而且由于Python是动态类型化的语言,因此您无需将函数的返回值显式转换为类型。 因此,除非ls从一开始就不是列表,否则在primeList = list(getPrimes())
和ls = list(ls)
行中, list
并不是必需的。 您最可能有该行,因为您的函数中有拆包运算符*
,然后它返回([1, 2, 3],)
,它是带有列表[1,2,3]
([1, 2, 3],)
的类型元组(,)
(“一对值”)。 [1,2,3]
作为第一个元素。
希望我能帮忙解决一些问题。 编程愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.