在python中过滤掉矩阵中的一些元素

时间:2016-08-10 10:01:59

标签: python

我有一个列表列表(矩阵)。我想在矩阵中过滤掉那些甚至一个0的列表,并将结果导出到.txt文件。

输入示例:

[['ENSG00000137288.5', '0,803921621', '0', '0,435897439', '1,384615397', '0,894736842', '1,151515086', '1,25', '1,2'], ['ENSG00000116032.5', '1,531746311', '2,67857148', '2', '3,0250002', '1,758620722', '1,571428459', '1,028571488', '1,294117703'], ['ENSG00000167578.12', '1,615720507', '2,21323528', '3,308642104', '3,934426129', '1,843137535', '0', '3,108108197', '3,33333321']]

输出示例,它将是.txt文件中的一行:

ENSG00000116032.5, 1,531746311, 2,67857148, 2, 3,0250002, 1,758620722, 1,571428459, 1,028571488, 1,294117703

感谢

1 个答案:

答案 0 :(得分:0)

我找到了一个方法,如果有帮助: -

    a = [['ENSG00000137288.5', '0,803921621', '0', '0,435897439', '1,384615397', '0,894736842', '1,151515086', '1,25', '1,2'], ['ENSG00000116032.5', '1,531746311', '2,67857148', '2', '3,0250002', '1,758620722', '1,571428459', '1,028571488', '1,294117703'], ['ENSG00000167578.12', '1,615720507', '2,21323528', '3,308642104', '3,934426129', '1,843137535', '0', '3,108108197', '3,33333321']]

    b = filter(lambda x : False if x.count('0') > 0 else x, a)

    print "\n".join([" ".join(item) for item in b])

    #op = 'ENSG00000116032.5 1,531746311 2,67857148 2 3,0250002 1,758620722 1,571428459 1,028571488 1,294117703'

使用此输出写入txt文件。