使用python从2D数组中删除空格

时间:2014-07-05 16:47:52

标签: python matrix multidimensional-array

如何使用python删除我的2D数组中的空格..我已经编写了如下代码。 代码:

 MI_feature_matrix_POS = [["" for x in xrange(2)] for x in xrange(1000)]
 ##### perform some opeation and assign some values into the matrix..and sort the matrix
 sorted_MI_feature_rank_list = sorted ( MI_feature_matrix, key=lambda MI_feature_matrix: MI_feature_matrix[0], reverse = False )
      sorted_MI_feature_rank_list_POS=filter(None, sorted_MI_feature_rank_list_POS)
      numrows = len(sorted_MI_feature_rank_list_POS)
      for i in range(0,numrows):
          print sorted_MI_feature_rank_list_POS[i]


> output:
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     []
>     ['addicted', '0.00010118008040085441']
>     ['admitted', '0.00010118008040085441']
>     ['age', '0.00010118008040085441']
>     ['anecdote', '0.00010118008040085441']
>     ['arguement', '0.00010118008040085441']
>     ['banana', '0.00010118008040085441']

...

我想从这个矩阵中删除这个空格?是否可能?

2 个答案:

答案 0 :(得分:1)

您可以使用没有函数参数的filter

filter(None, sorted_MI_feature_rank_list_POS)

答案 1 :(得分:1)

我的解决方案:

sorted_MI_feature_rank_list_POS = [i for i in sorted_MI_feature_rank_list_POS if i]