【问题标题】:How to remove index list from another list in python? [duplicate]如何从python中的另一个列表中删除索引列表? [复制]
【发布时间】:2017-03-05 03:00:22
【问题描述】:

我有两个长长的清单。我基本上想从这个列表中删除不匹配条件的元素。例如,

list_1=['a', 'b', 'c', 'd']

list_2=['1', 'e', '1', 'e']

列表一和二是相互对应的。现在我想从列表一中删除某些与我的条件不匹配的元素。我必须确保我从列表 2 中删除了相应的元素并且顺序不会混乱。

所以我创建了一个遍历列表 1 的 for 循环并存储所有必须删除的元素的索引。

假设:

index_list = ['1', '3']

基本上,我需要确保从列表 1 中删除 b 和 d,从列表 2 中删除 e 和 e。我该怎么做?

我试过了:

del (list_1 [i] for i in index_list)]

del (list_2 [i] for i in index_list)]

但我收到一个错误,即索引必须是一个列表,而不是列表。我也试过了:

list_1.remove[i]

list_2.remove[i]

但这也不起作用。我尝试创建另一个循环:

for e, in (list_1):

    for i, in (index_list):

        if e == i:

            del list_1(i)

for j, in (list_2):

    for i, in (index_list):

        if j == i:

            del list_2(i)

但这也不起作用。它给了我一个错误,即 e 和 j 不是全局名称。

【问题讨论】:

    标签: python list


    【解决方案1】:

    试试这个:

    >>> list_1=['a', 'b', 'c', 'd']
    >>> list_2 = ['1', 'e', '1', 'e']
    >>> index_list = ['1', '3']
    >>> index_list = [int(i) for i in index_list] # convert str to int for index
    >>> list_1 = [i for n, i in enumerate(list_1) if n not in index_list]
    >>> list_2 = [i for n, i in enumerate(list_2) if n not in index_list]
    >>> list_1
    ['a', 'c']
    >>> list_2
    ['1', '1']
    >>> 
    

    【讨论】:

      【解决方案2】:

      怎么样:

      list_1, list_2 = zip(*((x, y) for x, y in zip(list_1, list_2) if f(x)))
      

      其中f 是一个函数,用于测试list_1 中的某个值是否符合您的条件。

      例如:

      list_1 = ['a', 'b', 'c', 'd']
      list_2 = ['1', 'e', '1', 'e']
      
      
      def f(s):
          return s == 'b' or s == 'c'
      
      list_1, list_2 = zip(*((x, y) for x, y in zip(list_1, list_2) if f(x)))
      
      print list_1
      print list_2
      

      ('b', 'c')

      ('e', '1')

      (请注意,此方法实际上使list1list2 成为元组,这对于您的用例可能合适,也可能不合适。如果您真的需要它们成为列表,那么您可以很容易地将它们转换为列表行:

      list_1, list_2 = list(list_1), list(list_2)
      

      就在“主要”行之后。)

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        index_list.sort(reverse=True, key=int)
        for i in index_list:
            del(list_1[int(i)])
            del(list_2[int(i)])
        

        或者你也可以这样做:

        list_1 = [item for item in list_1 if f(item)]
        list_2 = [item for item in list_2 if f(item)]
        

        f 是一个根据您的标准返回 True/False 的函数。就像在您的示例中一样,def f(item): return item != 'a' and item != 'c' and item != 'e'

        【讨论】:

          【解决方案4】:

          聚会有点晚了,但这里有另一个版本。

          list_1=['a', 'b', 'c', 'd']
          
          list_2=['1', 'e', '1', 'e']
          index_list = ['1', '3']
          
          
          #convert index_list to int
          index_list = [ int(x) for x in index_list ]
          
          #Delete elements as per index_list from list_1
          new_list_1 = [i for i in list_1 if list_1.index(i) not in index_list]
          
          #Delete elements as per index_list from list_2
          new_list_2 = [i for i in list_2 if list_2.index(i) not in index_list]
          
          print "new_list_1=", new_list_1
          print "new_list_2=", new_list_2
          

          输出

          Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
          Type "copyright", "credits" or "license()" for more information.
          >>> ================================ RESTART ================================
          >>> 
          new_list_1= ['a', 'c']
          new_list_2= ['1', '1']
          >>> 
          

          【讨论】:

            猜你喜欢
            • 2016-09-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-20
            • 2011-01-31
            • 2022-06-16
            相关资源
            最近更新 更多