Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从数组中删除特定元素_Python_Arrays - Fatal编程技术网

如何使用python从数组中删除特定元素

如何使用python从数组中删除特定元素,python,arrays,Python,Arrays,我想写一些从数组中删除特定元素的东西。我知道我必须for在数组中循环以找到与内容匹配的元素 假设我有一个电子邮件数组,我想去掉与某个电子邮件字符串匹配的元素 实际上我想使用for循环结构,因为我还需要对其他数组使用相同的索引 以下是我的代码: for index, item in emails: if emails[index] == 'something@something.com': emails.pop(index) otherarray.pop

我想写一些从数组中删除特定元素的东西。我知道我必须
for
在数组中循环以找到与内容匹配的元素

假设我有一个电子邮件数组,我想去掉与某个电子邮件字符串匹配的元素

实际上我想使用for循环结构,因为我还需要对其他数组使用相同的索引

以下是我的代码:

for index, item in emails:
    if emails[index] == 'something@something.com':
         emails.pop(index)
         otherarray.pop(index)

您不需要迭代数组。只是:

>>> x = ['ala@ala.com', 'bala@bala.com']
>>> x
['ala@ala.com', 'bala@bala.com']
>>> x.remove('ala@ala.com')
>>> x
['bala@bala.com']
这将删除与字符串匹配的第一次出现

编辑:编辑之后,仍然不需要迭代。只要做:

index = initial_list.index(item1)
del initial_list[index]
del other_list[index]

正确的方法是使用
zip()
和列表理解/生成器表达式:

filtered = (
    (email, other) 
        for email, other in zip(emails, other_list) 
            if email == 'something@something.com')

new_emails, new_other_list = zip(*filtered)

此外,如果您没有使用
array.array()
numpy.array()
,则很可能您使用的是
[]
list()
,它们提供的是列表,而不是数组。不一样。

如果需要在for循环中使用索引,则for循环不正确:

for index, item in enumerate(emails):
    # whatever (but you can't remove element while iterating)
在您的情况下,Bogdan解决方案是可以的,但您的数据结构选择不是很好。必须在同一索引中维护这两个列表,其中一个列表的数据与另一个列表的数据相关,这是很笨拙的

一个元组列表(电子邮件、其他数据)可能更好,或者一个以电子邮件为键的dict

使用
filter()
lambda
将提供一种简洁的方法来删除不需要的值:

newEmails = list(filter(lambda x : x != 'something@something.com', emails))

这不会修改电子邮件。它创建新的列表newemail,其中只包含匿名函数返回True的元素。

此问题还有一个替代解决方案,它还处理重复匹配

我们从两个长度相等的列表开始:
电子邮件
其他数组
。目标是从两个列表中删除每个索引
i
where
emails[i]='something@something.com“

这可以通过列表理解,然后通过
zip
进行拆分来实现:

emails = ['abc@def.com', 'something@something.com', 'ghi@jkl.com']
otherarray = ['some', 'other', 'details']

from operator import itemgetter

res = [(i, j) for i, j in zip(emails, otherarray) if i!= 'something@something.com']
emails, otherarray = map(list, map(itemgetter(0, 1), zip(*res)))

print(emails)      # ['abc@def.com', 'ghi@jkl.com']
print(otherarray)  # ['some', 'details']
如果要删除数组的索引,请执行以下操作: 使用数组名.pop(索引号)

例:-

如果要从数组中删除特定的字符串/元素,则
你在找什么?不完全是。我想使用for循环,这样我就可以重用索引。你不应该在遍历列表时更改它。为什么我不应该这样做?另外,它对我也不起作用。请注意:如上所述,我想使用for循环来重用我的答案中相同的索引。仍然不需要循环。如何首先检查初始列表中是否存在该项?可能有一种情况,它不存在,您不必“删除它”。@locoboy 2选项。要么测试初始列表中的
,要么用
try:code:except ValueError
将删除项包围起来。与@Bogdan的答案相比,没有人确定这是否“合理”,因为后者要干净得多。感谢您指出数组与列表不同。所选答案在2.7中的数组上不起作用。
>>> arr = [1,2,3,4]
>>> arr.pop(2)
>>>arr
[1,2,4]
>>> arr1 = ['python3.6' , 'python2' ,'python3']
>>> arr1.remove('python2')
>>> arr1
['python3.6','python3']