【问题标题】:Python - removing items in multidimensional arrayPython - 删除多维数组中的项目
【发布时间】:2017-05-11 23:33:28
【问题描述】:

我有一个像这样的多维数组:

[["asdf","bmnl", "123","456,"0","999","1234","3456"],["qwer","tyui","789","657,"122","9","673","1"]]

但是,在多维数组中,每个数组只需要最后6项,不需要前两项。如何从多维数组中的每个数组中删除前两条数据,使其看起来像:

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]

到目前为止,我已经这样做了:

list1 = []
list2 = []
for row in rows:
    list1.append(row[0].split(',')) #to put the split list into the top i i.e. [["asdf","bmnl", "123","456,"0","999","1234","3456"]["qwer","tyui","789","657,"122","9","673","1"]]
for i in list1:
    for index in len(list1):
        if index >=2:
             list2.append(index) #this does not work and causes errors

我该如何解决这个问题,所以输出将是:

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]

谢谢

【问题讨论】:

    标签: python arrays multidimensional-array append


    【解决方案1】:

    您可以使用list comprehension,如下所示:

    [item[2:] for item in my_list]
    

    item[2:] 称为列表切片,意思是对于my_list 的每个子列表,我们从索引2 中取出items 直到最后一项。

    输出:

    >>> my_list = [["asdf", "bmnl", "123", "456", "0", "999", "1234", "3456"], ["qwer", "tyui", "789", "657", "122", "9", "673", "1"]]
    >>>
    >>> [item[2:] for item in my_list]
    [['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]
    

    【讨论】:

      【解决方案2】:

      这是list comprehension 的典型用例:

      list2 = [item[2:] for item in list1]
      

      【讨论】:

        【解决方案3】:

        只需使用列表推导式并从索引 2 中获取每个子列表中的每个元素:

        >>> array = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
        >>> [sublist[2:] for sublist in array]
        [['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]
        

        【讨论】:

          【解决方案4】:
          start = [["asdf","bmnl", "123","456","0","999","1234","3456"],
                   ["qwer","tyui","789","657","122","9","673","1"]]
          
          list_1, list_2 = [i[2:] for i in start] # I use list comprehension to create a multidimensional 
                                                  # list that contains slices of each object in the base 
                                                  # list and unwrap it to list_1 and list_2
          

          同

          n_list = [] #create an empty new list
          for i in start: #loop through the origional
              n_list.append(i[2:]) #append slices of each item to the new list
          list_1, list_2 = n_list #unwrap the list
          

          【讨论】:

          • 请在代码中包含一些解释,以便其他人可以理解它是如何工作的
          【解决方案5】:
          lst = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
          for i in lst:
              del i[0:2] #deleting 0 and 1 index from each list
          
          print lst
          

          【讨论】:

            猜你喜欢
            • 2017-01-01
            • 1970-01-01
            • 2012-12-14
            • 1970-01-01
            • 1970-01-01
            • 2021-11-07
            • 1970-01-01
            • 2014-09-03
            • 1970-01-01
            相关资源
            最近更新 更多