【问题标题】:remove white spaces from a 2D array using python使用python从二维数组中删除空格
【发布时间】:2014-07-05 16:47:52
【问题描述】:

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

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

....

我想从这个矩阵中删除这个空格?可以吗??

【问题讨论】:

  • 你在哪里使用MI_feature_matrix_POS
  • 这是我排序前的原始矩阵..

标签: python matrix multidimensional-array


【解决方案1】:

您可以使用不带函数参数的filter

filter(None, sorted_MI_feature_rank_list_POS)

【讨论】:

  • 我在问题中看到了同样的说法。
  • 如果您很好奇,这是因为空列表 [] 的计算结果为 False,因此您无需指定 lambda 函数,列表的自然语义为您工作。跨度>
  • @thefourtheye 我认为他更新了问题以包含此答案
【解决方案2】:

我的解决方案:

sorted_MI_feature_rank_list_POS = [i for i in sorted_MI_feature_rank_list_POS if i]

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2019-08-05
    • 2017-11-06
    • 2012-01-21
    相关资源
    最近更新 更多