【问题标题】:How to remove specific element from an array using python如何使用python从数组中删除特定元素
【发布时间】:2011-10-30 09:40:15
【问题描述】:

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

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

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

这是我的代码:

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

【问题讨论】:

标签: python arrays


【解决方案1】:

如果要删除数组的索引:

使用array_name.pop(index_no.)

例如:-

>>> arr = [1,2,3,4]
>>> arr.pop(2)
>>>arr
[1,2,4]

如果你想从数组中删除特定的字符串/元素,那么

>>> arr1 = ['python3.6' , 'python2' ,'python3']
>>> arr1.remove('python2')
>>> arr1
['python3.6','python3']

【讨论】:

    【解决方案2】:

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

    >>> 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]
    

    【讨论】:

    • 见上文我想使用for循环来重用相同的索引
    • 编辑了我的答案。仍然不需要循环。
    • 如何首先检查该项目是否存在于 initial_list 中?可能存在它不存在的情况,您不必将其删除。
    • @locoboy 2 个选项。要么测试item in initial_list,要么用try: code : except ValueError包围删除
    【解决方案3】:

    这个问题有一个替代解决方案,它也可以处理重复匹配。

    我们从 2 个长度相等的列表开始:emailsotherarray。目标是从两个列表中删除每个索引i 的项目,其中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']
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        你的for循环不对,如果你需要在for循环中使用索引:

        for index, item in enumerate(emails):
            # whatever (but you can't remove element while iterating)
        

        在您的情况下,Bogdan 解决方案还可以,但您的数据结构选择不太好。必须使用来自同一索引的另一个相关数据的数据来维护这两个列表是很笨拙的。

        元组列表(电子邮件,其他数据)可能更好,或者以电子邮件为键的字典。

        【讨论】:

          【解决方案6】:

          明智的做法是使用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(),它们会为您提供列表,而不是数组。不是一回事。

          【讨论】:

          • 与@Bogdan 的回答相比,不知道这有多“理智”,后者干净得多。
          • 感谢您指出数组与列表不同。所选答案不适用于 2.7 中的数组。
          猜你喜欢
          • 1970-01-01
          • 2011-01-27
          • 2019-06-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多