过滤元组Python

时间:2017-03-15 19:45:57

标签: python python-3.x numpy

我有一个包含四列/功能的元组,并希望根据列的值过滤行。

score =[a for a in tup if a[1] == 0.1]

输出为print(score)

[array([ -2.41888055e-01,   1.00000000e-02,   1.00000000e-08,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   2.39502662e-08,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   5.73615251e-08,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   1.37382380e-07,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   3.29034456e-07,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   7.88046282e-07,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   1.88739182e-06,
     1.00000000e-05]), array([ -2.41888054e-01,   1.00000000e-02,   4.52035366e-06,
     1.00000000e-05]), array([ -2.41888054e-01,   1.00000000e-02,   1.08263673e-05,
     1.00000000e-05]), array([ -2.41888053e-01,   1.00000000e-02,   2.59294380e-05,
     1.00000000e-05]), array([ -2.41888050e-01,   1.00000000e-02,   6.21016942e-05,
     1.00000000e-05]), array([ -2.41888043e-01,   1.00000000e-02,   1.48735211e-04,
     1.00000000e-05]), array([ -2.41888026e-01,   1.00000000e-02,   3.56224789e-04,
     1.00000000e-05]), array([ -2.41887986e-01,   1.00000000e-02,   8.53167852e-04,
     1.00000000e-05]), array([ -2.41887896e-01,   1.00000000e-02,   2.04335972e-03,
     1.00000000e-05]), array([ -2.41887705e-01,   1.00000000e-02,   4.89390092e-03,
     1.00000000e-05]), array([ -2.41887377e-01,   1.00000000e-02,   1.17210230e-02,
     1.00000000e-05]), array([ -2.41887150e-01,   1.00000000e-02,   2.80721620e-02,
     1.00000000e-05]), array([ -2.41888211e-01,   1.00000000e-02,   6.72335754e-02,
     1.00000000e-05]), array([ -2.41891897e-01,   1.00000000e-02,   1.61026203e-01,
     1.00000000e-05]), array([ -2.41895321e-01,   1.00000000e-02,   3.85662042e-01,
     1.00000000e-05]), array([ -2.41895312e-01,   1.00000000e-02,   9.23670857e-01,
     1.00000000e-05]), array([ -2.41892873e-01,   1.00000000e-02,   2.21221629e+00,
     1.00000000e-05]), array([ -2.41889564e-01,   1.00000000e-02,   5.29831691e+00,
     1.00000000e-05]), array([ -2.41888135e-01,   1.00000000e-02,   1.26896100e+01,
     1.00000000e-05]), array([ -2.41888055e-01,   1.00000000e-02,   3.03919538e+01,

我想将所有这些单独的数组组合回一个元组,同时保留值的序列。请帮助我并提供一种过滤此元组的方法。

2 个答案:

答案 0 :(得分:0)

您可以减少将它们组合回一个

success

答案 1 :(得分:0)

您可以使用Numpy执行此操作,而无需使用numpy.where()

转到其他数据类型

一些假设我假设要比较的数字是问题中的0.01比0.1。

import numpy as np
#put your data (tup) as a numpy array here
tup[np.where(tup[:,1]==0.01)]