python - Selecting elements from a .dat file according to an assigned value -
i have .dat file of form:
1.5699000000000001 -10.230000000000000 3 1.5978000000000001 -10.550000000000001 3 1.2388999999999999 -9.7100000000000009 3 1.9540999999999999 -12.910000000000000 4 1.7915000000000001 -12.539999999999999 4 1.7547999999999999 -10.740000000000000 4 1.8927000000000000 -10.800000000000001 4 1.9222999999999999 -10.869999999999999 4 1.9047000000000001 -11.770000000000000 4 1.8488000000000000 -12.279999999999999 4 1.6536000000000000 -10.690000000000000 3 1.7059000000000000 -11.410000000000000 4 1.7834000000000001 -12.250000000000000 4 1.4136000000000000 -9.7699999999999996 3 1.8093999999999999 -10.990000000000000 4 1.4621999999999999 -10.490000000000000 4 1.4086000000000001 -10.670000000000000 3 1.5290999999999999 -10.660000000000000 3 1.5990000000000000 -10.619999999999999 3 1.9164000000000001 -12.580000000000000 4 1.9463999999999999 -12.580000000000000 4 1.6439999999999999 -12.340000000000000 4 1.8986000000000001 -12.270000000000000 4 1.4316000000000000 -10.029999999999999 4 1.8346000000000000 -11.000000000000000 4 1.3900999999999999 -10.650000000000000 3
firstly, wondering how set numpy array, perhaps utilise fromfile
numpy module.
secondly, how separate further 2 arrays, 1 elements containing '3' , 1 '4'...?
plz use genfromtxt()
:
in [8]: import numpy np = np.genfromtxt('your_file.dat') in [9]: print a[a[:,2]==3] [[ 1.5699 -10.23 3. ] [ 1.5978 -10.55 3. ] [ 1.2389 -9.71 3. ] [ 1.6536 -10.69 3. ] [ 1.4136 -9.77 3. ] [ 1.4086 -10.67 3. ] [ 1.5291 -10.66 3. ] [ 1.599 -10.62 3. ] [ 1.3901 -10.65 3. ]] in [10]: print a[a[:,2]==4] [[ 1.9541 -12.91 4. ] [ 1.7915 -12.54 4. ] [ 1.7548 -10.74 4. ] [ 1.8927 -10.8 4. ] [ 1.9223 -10.87 4. ] [ 1.9047 -11.77 4. ] [ 1.8488 -12.28 4. ] [ 1.7059 -11.41 4. ] [ 1.7834 -12.25 4. ] [ 1.8094 -10.99 4. ] [ 1.4622 -10.49 4. ] [ 1.9164 -12.58 4. ] [ 1.9464 -12.58 4. ] [ 1.644 -12.34 4. ] [ 1.8986 -12.27 4. ] [ 1.4316 -10.03 4. ] [ 1.8346 -11. 4. ]]
Comments
Post a Comment