I have a list of list of list in the following format:
[ [[a1_1, a1_2, a1_3, a1_4], [b1_1, b1_2, b1_3, b1_4]],
[[a2_1, a2_2, a2_3, a2_4], [b2_1, b2_2, b2_3, b2_4]],
:
:
[[a10_1, a10_2, a10_3, a10_4], [b10_1, b10_2, b10_3, b10_4]] ]
Except iterate over each element and add it to the new structure, is there an elegant way to accomplish the following:
Restructure the list to:
[ [[ a1_1, b1_1], [a1_2, b1_2], [a1_3, b1_3], [a1_4, b1_4]],
[[ a2_1, b2_1], [a2_2, b2_2], [a2_3, b2_3], [a2_4, b2_4]],
:
:
[[ a10_1, b10_1], [a10_2, b10_2], [a10_3, b10_3], [a10_4, b10_4]] ]
Then convert the above list of list of list to numpy structure in the shape of 10 x 4 x 2. Thanks!
You can use tranpose here:
import numpy as np
ar = np.array(data)
and then:
ar.transpose((0,2,1))
or equivalent:
ar.transpose(0,2,1)
If I write strings into the variables, and then use your sample data, I get:
>>> ar
array([[['a_1_1', 'a_1_2', 'a_1_3', 'a_1_4'],
['b_1_1', 'b_1_2', 'b_1_3', 'b_1_4']],
[['a_2_1', 'a_2_2', 'a_2_3', 'a_2_4'],
['b_2_1', 'b_2_2', 'b_2_3', 'b_2_4']],
[['a_10_1', 'a_10_2', 'a_10_3', 'a_10_4'],
['b_10_1', 'b_10_2', 'b_10_3', 'b_10_4']]],
dtype='<U6')
>>> ar.transpose((0,2,1))
array([[['a_1_1', 'b_1_1'],
['a_1_2', 'b_1_2'],
['a_1_3', 'b_1_3'],
['a_1_4', 'b_1_4']],
[['a_2_1', 'b_2_1'],
['a_2_2', 'b_2_2'],
['a_2_3', 'b_2_3'],
['a_2_4', 'b_2_4']],
[['a_10_1', 'b_10_1'],
['a_10_2', 'b_10_2'],
['a_10_3', 'b_10_3'],
['a_10_4', 'b_10_4']]],
dtype='<U6')
transpose takes as input an array and a list of indices. It rearanges the indices such that (in case we give it (0,2,1)), the old first (0) dimension; is the new first dimension, the old third (2) dimension, is the new second dimension, and the old second (1) dimension is the new third dimension.
If you already have a list, you should be able to accomplish this relatively painlessly, just use the zip transpose idiom on the sublists:
arr = np.array([list(zip(*sub)) for sub in my_list])
So, using only 3-rows...
In [1]: data = [ [['a1_1', 'a1_2', 'a1_3', 'a1_4'], ['b1_1', 'b1_2', 'b1_3', 'b1_4']],
...: [['a2_1', 'a2_2', 'a2_3', 'a2_4'], ['b2_1', 'b2_2', 'b2_3', 'b2_4']],
...: [['a10_1', 'a10_2', 'a10_3', 'a10_4'], ['b10_1', 'b10_2', 'b10_3', 'b10_4']] ]
In [2]: [list(zip(*sub)) for sub in data]
Out[2]:
[[('a1_1', 'b1_1'), ('a1_2', 'b1_2'), ('a1_3', 'b1_3'), ('a1_4', 'b1_4')],
[('a2_1', 'b2_1'), ('a2_2', 'b2_2'), ('a2_3', 'b2_3'), ('a2_4', 'b2_4')],
[('a10_1', 'b10_1'), ('a10_2', 'b10_2'), ('a10_3', 'b10_3'), ('a10_4', 'b10_4')]]
In [3]: import numpy as np
In [4]: np.array([list(zip(*sub)) for sub in data])
Out[4]:
array([[['a1_1', 'b1_1'],
['a1_2', 'b1_2'],
['a1_3', 'b1_3'],
['a1_4', 'b1_4']],
[['a2_1', 'b2_1'],
['a2_2', 'b2_2'],
['a2_3', 'b2_3'],
['a2_4', 'b2_4']],
[['a10_1', 'b10_1'],
['a10_2', 'b10_2'],
['a10_3', 'b10_3'],
['a10_4', 'b10_4']]],
dtype='<U5')
In [5]: np.array([list(zip(*sub)) for sub in data]).shape
Out[5]: (3, 4, 2)
Related
I am a python beginner, please help me with this python case.
Sort given 2d array in order of ascending.Flatten the 2D array, and sort it such that first sort order is the first number, second sort order is the second number'''# Example:
input_arr = [
['55-29', '55-32', '62-3', '84-38'],
['36-84', '23-53', '22-58', '48-15'],
['72-80', '48-6', '11-86', '73-23'],
['93-51', '55-11', '93-49', '72-10'],
['93-66', '71-32', '16-75', '55-9']
]
ouput_arr = ['11-86', '16-75', '22-58', '23-53', '36-84', '48-6', '48-15', '55-9', '55-11', '55-29', '55-32', '62-3', '71-32', '72-10', '72-80', '73-23', '84-38', '93-49', '93-51', '93-66']
def sort_2d_array(input_arr=input_arr) -> list:
#TODO
pass
Try this
you need to flatten your list into single list like this
tmp = [t for x in input_arr for t in x]
then do the sorting based on first element before the -, like this
print(list(sorted(tmp,key=lambda x: int(x.split('-')[0]))))
this will give you your desired output.
['11-86', '16-75', '22-58', '23-53', '36-84', '48-15', '48-6', '55-29', '55-32', '55-11', '55-9', '62-3', '71-32', '72-80', '72-10', '73-23', '84-38', '93-51', '93-49', '93-66']
for second condition you can try
print(list(sorted(tmp,key=lambda x: (int(x.split('-')[0]) , int(x.split('-')[1])))))
this will give you
['11-86', '16-75', '22-58', '23-53', '36-84', '48-6', '48-15', '55-9', '55-11', '55-29', '55-32', '62-3', '71-32', '72-10', '72-80', '73-23', '84-38', '93-49', '93-51', '93-66']
I have a block of string as below. How do I read this into a numpy array?
5.780326E+03 7.261185E+03 7.749190E+03 8.488770E+03 5.406134E+03 2.828410E+03 9.620957E+02 1.0000000E+00
3.097372E+03 3.885160E+03 5.432678E+03 8.060628E+03 2.768457E+03 6.574258E+03 7.268591E+02 2.0000000E+00
2.061429E+03 4.665282E+03 8.214119E+03 3.579380E+03 8.542057E+03 2.089062E+03 8.829263E+02 3.0000000E+00
3.572444E+03 9.920473E+03 3.573251E+03 6.423813E+03 2.469338E+03 4.652253E+03 8.211962E+02 4.0000000E+00
7.460966E+03 7.691966E+03 7.501826E+03 3.414511E+03 8.590221E+03 6.737868E+03 8.586273E+02 5.0000000E+00
3.250046E+03 9.611985E+03 9.195165E+03 1.064800E+03 7.944535E+03 2.685740E+03 8.212849E+02 6.0000000E+00
8.069926E+03 9.208576E+03 4.267749E+03 2.491888E+03 9.036555E+03 5.001732E+03 7.202407E+02 7.0000000E+00
5.691460E+03 3.868344E+03 3.103342E+03 6.567618E+03 7.274860E+03 8.393253E+03 5.628069E+02 8.0000000E+00
2.887292E+03 9.081563E+02 6.955551E+03 6.763133E+03 2.146178E+03 2.033861E+03 9.725472E+02 9.0000000E+00
6.127778E+03 8.065057E+02 7.474341E+03 4.185868E+03 4.516230E+03 8.714840E+03 8.254562E+02 1.0000000E+01
1.594643E+03 6.060956E+03 2.137153E+03 3.505950E+03 7.714227E+03 6.249693E+03 5.724376E+02 1.1000000E+01
5.039059E+03 3.138161E+03 5.570104E+03 4.594189E+03 7.889644E+03 1.891062E+03 7.085753E+02 1.2000000E+01
3.263593E+03 6.085087E+03 7.136061E+03 9.895028E+03 6.139666E+03 6.670919E+03 5.018248E+02 1.3000000E+01
9.954830E+03 6.777074E+03 3.013747E+03 3.638458E+03 4.357685E+03 1.876539E+03 5.969378E+02 1.4000000E+01
9.920853E+03 3.414156E+03 5.534430E+03 2.011815E+03 7.791122E+03 3.893439E+03 5.229754E+02 1.5000000E+01
5.447470E+03 7.184321E+03 1.382575E+03 9.134295E+03 7.883753E+02 9.160537E+03 7.521197E+02 1.6000000E+01
3.344917E+03 8.151884E+03 3.596052E+03 3.953284E+03 7.456115E+03 7.749632E+03 9.773521E+02 1.7000000E+01
6.310496E+03 1.472792E+03 1.812452E+03 9.535100E+03 1.581263E+03 3.649150E+03 6.562440E+02 1.8000000E+01
I am trying to use numpy native methods so as to speed up the data reading. I am trying to read in couple of GBs of data from a custom file format. I am able to seek and reach the area where a block of text as shown above will appear. Doing regular python string operations on this is always possible, however, I wanted to know if there is any native numpy methods to read in fixed width format.
I tried using np.frombuffer with dtype=float which did not work. It seems to read if I use dtype='S15' however, shows up as bytes and not numbers.
In [294]: txt = """5.780326E+03 7.261185E+03 7.749190E+03 8.488770E+03 5.406134E+03 2
...: .828410E+03 9.620957E+02 1.0000000E+00
...: 3.097372E+03 3.885160E+03 5.432678E+03 8.060628E+03 2.768457E+03 6.57425
...: 8E+03 7.268591E+02 2.0000000E+00
...: 2.061429E+03 4.665282E+03 8.214119E+03 3.579380E+03 8.542057E+03 2.08906
...: 2E+03 8.829263E+02 3.0000000E+00
...: """
With this copy-n-paste I'm assuming your block is a multiline string.
Treating it like a csv file.
In [296]: np.loadtxt(txt.splitlines())
Out[296]:
array([[5.780326e+03, 7.261185e+03, 7.749190e+03, 8.488770e+03,
5.406134e+03, 2.828410e+03, 9.620957e+02, 1.000000e+00],
[3.097372e+03, 3.885160e+03, 5.432678e+03, 8.060628e+03,
2.768457e+03, 6.574258e+03, 7.268591e+02, 2.000000e+00],
[2.061429e+03, 4.665282e+03, 8.214119e+03, 3.579380e+03,
8.542057e+03, 2.089062e+03, 8.829263e+02, 3.000000e+00]])
There's a lot going on under the covers, so this isn't particularly fast. pandas has a faster csv reader.
fromstring works, but returns 1d. You can reshape the result
n [299]: np.fromstring(txt, sep=' ')
Out[299]:
array([5.780326e+03, 7.261185e+03, 7.749190e+03, 8.488770e+03,
5.406134e+03, 2.828410e+03, 9.620957e+02, 1.000000e+00,
3.097372e+03, 3.885160e+03, 5.432678e+03, 8.060628e+03,
2.768457e+03, 6.574258e+03, 7.268591e+02, 2.000000e+00,
2.061429e+03, 4.665282e+03, 8.214119e+03, 3.579380e+03,
8.542057e+03, 2.089062e+03, 8.829263e+02, 3.000000e+00])
This is a string, not a buffer, so frombuffer is wrong.
This list comprehension works:
np.array([row.strip().split(' ') for row in txt.strip().splitlines()], float)
I had to add strip to clear out excess blanks that produced empty lists or strings.
At least with this small sample, the list comprehension isn't that much slower than the fromstring, and still a lot better than the more general loadtxt.
You could use several string operations to convert the the data to a string which is convertible to float. Such as:
import numpy as np
with open('data.txt', 'r') as f:
data = f.readlines()
result = []
for line in data:
splitted_data = line.split(' ')
splitted_data = [item for item in splitted_data if item]
splitted_data = [item.replace('E+', 'e') for item in splitted_data]
result.append(splitted_data)
result = np.array(result, dtype = 'float64')
Where data.txt is the data you pasted in your question.
I just did a regular python split and assigned the dtype to np.float32
>>> y=np.array(x.split(), dtype=np.float32())
>>> y
array([ 5.78032617e+03, 7.26118506e+03, 7.74918994e+03,
8.48876953e+03, 5.40613379e+03, 2.82840991e+03,
9.62095703e+02, 1.00000000e+00, 3.09737207e+03,
3.88515991e+03, 5.43267822e+03, 8.06062793e+03,
2.76845703e+03, 6.57425781e+03, 7.26859070e+02,
2.00000000e+00, 2.06142896e+03, 4.66528223e+03,
8.21411914e+03, 3.57937988e+03, 8.54205664e+03,
2.08906201e+03, 8.82926270e+02, 3.00000000e+00], dtype=float32)
P.S. I copied a chunk of your sample data and assigned it to variable “x”
Ok, this doesn’t rely on any blank spaces or use split(), except for the lines, and maintains the shape of the array but does still use non Numpy python.
>>> n=15
>>> x=' 5.780326E+03 7.261185E+03 7.749190E+03 8.488770E+03 5.406134E+03 2.828410E+03 9.620957E+02 1.0000000E+00\n 3.097372E+03 3.885160E+03 5.432678E+03 8.060628E+03 2.768457E+03 6.574258E+03 7.268591E+02 2.0000000E+00\n 2.061429E+03 4.665282E+03 8.214119E+03 3.579380E+03 8.542057E+03 2.089062E+03 8.829263E+02 3.0000000E+00\n 3.572444E+03 9.920473E+03 3.573251E+03 6.423813E+03 2.469338E+03 4.652253E+03 8.211962E+02 4.0000000E+00\n 7.460966E+03 7.691966E+03 7.501826E+03 3.414511E+03 8.590221E+03 6.737868E+03 8.586273E+02 5.0000000E+00\n 3.250046E+03 9.611985E+03 9.195165E+03 1.064800E+03 7.944535E+03 2.685740E+03 8.212849E+02 6.0000000E+00\n 8.069926E+03 9.208576E+03 4.267749E+03 2.491888E+03 9.036555E+03 5.001732E+03 7.202407E+02 7.0000000E+00\n 5.691460E+03 3.868344E+03 3.103342E+03 6.567618E+03 7.274860E+03 8.393253E+03 5.628069E+02 8.0000000E+00\n 2.887292E+03 9.081563E+02 6.955551E+03 6.763133E+03 2.146178E+03 2.033861E+03 9.725472E+02 9.0000000E+00\n 6.127778E+03 8.065057E+02 7.474341E+03 4.185868E+03 4.516230E+03 8.714840E+03 8.254562E+02 1.0000000E+01\n 1.594643E+03 6.060956E+03 2.137153E+03 3.505950E+03 7.714227E+03 6.249693E+03 5.724376E+02 1.1000000E+01\n 5.039059E+03 3.138161E+03 5.570104E+03 4.594189E+03 7.889644E+03 1.891062E+03 7.085753E+02 1.2000000E+01\n 3.263593E+03 6.085087E+03 7.136061E+03 9.895028E+03 6.139666E+03 6.670919E+03 5.018248E+02 1.3000000E+01\n 9.954830E+03 6.777074E+03 3.013747E+03 3.638458E+03 4.357685E+03 1.876539E+03 5.969378E+02 1.4000000E+01\n 9.920853E+03 3.414156E+03 5.534430E+03 2.011815E+03 7.791122E+03 3.893439E+03 5.229754E+02 1.5000000E+01\n 5.447470E+03 7.184321E+03 1.382575E+03 9.134295E+03 7.883753E+02 9.160537E+03 7.521197E+02 1.6000000E+01\n 3.344917E+03 8.151884E+03 3.596052E+03 3.953284E+03 7.456115E+03 7.749632E+03 9.773521E+02 1.7000000E+01\n 6.310496E+03 1.472792E+03 1.812452E+03 9.535100E+03 1.581263E+03 3.649150E+03 6.562440E+02 1.8000000E+01'
>>> s=np.array([[y[i:i+n] for i in range(0, len(y) - n + 1, n)] for y in x.splitlines()], dtype=np.float32)
>>> s
array([[ 5.78032617e+03, 7.26118506e+03, 7.74918994e+03,
8.48876953e+03, 5.40613379e+03, 2.82840991e+03,
9.62095703e+02, 1.00000000e+00],
[ 3.09737207e+03, 3.88515991e+03, 5.43267822e+03,
8.06062793e+03, 2.76845703e+03, 6.57425781e+03,
7.26859070e+02, 2.00000000e+00],
[ 2.06142896e+03, 4.66528223e+03, 8.21411914e+03,
3.57937988e+03, 8.54205664e+03, 2.08906201e+03,
8.82926270e+02, 3.00000000e+00],
[ 3.57244409e+03, 9.92047266e+03, 3.57325098e+03,
6.42381299e+03, 2.46933789e+03, 4.65225293e+03,
8.21196228e+02, 4.00000000e+00],
[ 7.46096582e+03, 7.69196582e+03, 7.50182617e+03,
3.41451099e+03, 8.59022070e+03, 6.73786816e+03,
8.58627319e+02, 5.00000000e+00],
[ 3.25004590e+03, 9.61198535e+03, 9.19516504e+03,
1.06480005e+03, 7.94453516e+03, 2.68573999e+03,
8.21284912e+02, 6.00000000e+00],
[ 8.06992578e+03, 9.20857617e+03, 4.26774902e+03,
2.49188794e+03, 9.03655469e+03, 5.00173193e+03,
7.20240723e+02, 7.00000000e+00],
[ 5.69145996e+03, 3.86834399e+03, 3.10334204e+03,
6.56761816e+03, 7.27485986e+03, 8.39325293e+03,
5.62806885e+02, 8.00000000e+00],
[ 2.88729199e+03, 9.08156311e+02, 6.95555078e+03,
6.76313281e+03, 2.14617798e+03, 2.03386096e+03,
9.72547180e+02, 9.00000000e+00],
[ 6.12777783e+03, 8.06505676e+02, 7.47434082e+03,
4.18586816e+03, 4.51622998e+03, 8.71483984e+03,
8.25456177e+02, 1.00000000e+01],
[ 1.59464294e+03, 6.06095605e+03, 2.13715308e+03,
3.50594995e+03, 7.71422705e+03, 6.24969287e+03,
5.72437622e+02, 1.10000000e+01],
[ 5.03905908e+03, 3.13816089e+03, 5.57010400e+03,
4.59418896e+03, 7.88964404e+03, 1.89106201e+03,
7.08575317e+02, 1.20000000e+01],
[ 3.26359302e+03, 6.08508691e+03, 7.13606104e+03,
9.89502832e+03, 6.13966602e+03, 6.67091895e+03,
5.01824799e+02, 1.30000000e+01],
[ 9.95483008e+03, 6.77707422e+03, 3.01374707e+03,
3.63845801e+03, 4.35768506e+03, 1.87653894e+03,
5.96937805e+02, 1.40000000e+01],
[ 9.92085254e+03, 3.41415601e+03, 5.53443018e+03,
2.01181494e+03, 7.79112207e+03, 3.89343896e+03,
5.22975403e+02, 1.50000000e+01],
[ 5.44747021e+03, 7.18432080e+03, 1.38257495e+03,
9.13429492e+03, 7.88375305e+02, 9.16053711e+03,
7.52119690e+02, 1.60000000e+01],
[ 3.34491699e+03, 8.15188379e+03, 3.59605200e+03,
3.95328394e+03, 7.45611523e+03, 7.74963184e+03,
9.77352112e+02, 1.70000000e+01],
[ 6.31049609e+03, 1.47279199e+03, 1.81245203e+03,
9.53509961e+03, 1.58126294e+03, 3.64914990e+03,
6.56244019e+02, 1.80000000e+01]], dtype=float32)
Thanks to #hpaulj's comments. Here's the answer I ended up with.
data = np.genfromtxt(f, delimiter=[15]*8, max_rows=18)
More explanation
Since I am reading this from a custom file format, I will post how I'm doing the whole thing as well.
I do some initial processing of the file to identify the positions where the block of text is residing and end up with an array of 'locations' where I can seek to start the reading process and then I use the above method to read the 'block' of text.
data = np.array([])
r = 18 # rows per block
c = 8 # columns per block
w = 15 # width of a column
with open('mycustomfile.xyz') as f:
for location in locations:
f.seek(location)
data = np.append(data, np.genfromtxt(f, delimiter=[w]*c, max_rows=r))
data = data.reshape((r*len(locations),c))
If you want an array with dtype=float you have to convert your string to float beforehand.
import numpy as np
string_list = ["1", "0.1", "1.345e003"]
array = np.array([float(string) for string in string_list])
array.dtype
Initial data is:
array([[0.0417634 ],
[0.04493844],
[0.04932728],
[0.04601787],
[0.04511007],
[0.04312284],
[0.0451733 ],
[0.04560687],
[0.04263394],
[0.04183227],
[0.048634 ],
[0.05198746],
[0.05615724],
[0.05787913], dtype=float32)
then i transformed it in 2d array
array2d = np.reshape(dataset, (-1, 2))
now i have
array([[0.0417634 , 0.04493844],
[0.04932728, 0.04601787],
[0.04511007, 0.04312284],
[0.0451733 , 0.04560687],
[0.04263394, 0.04183227],
[0.048634 , 0.05198746],
[0.05615724, 0.05787913],
[0.05989346, 0.0605077 ], dtype=float32)
Now i'm going to calcolulate the mean between each element of the array
paa = []
paa.append(array2d.mean(axis=1))
now i want a list of intervals from this list
intervals = paa[::10]
intervals
but the result is the same list (paa). Why? Already tried to convert it in np.array(paa)
Expected a new list with less elements. Since 10 is the nr of steps i'm expecting [0.0417634, ... paa[11], .... paa[21] .... ]
np.mean will return a np.array. You are taking the result and appending it into a list. When you are slicing it, you're getting the 0th (and only) element in paa, which is an entire np.array.
Get rid of the list and append and slice directly into the result of mean.
I have an output as a list on which I need to perform some operations. For that, I converted into an array using np.asarrayfunction .
My list
[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
-5.41511240e+01, -2.30953821e+01, -1.74871288e+01,
-4.91000564e+01, 2.44382720e+02, 1.71213983e+02,
-1.54755310e+02, 5.97881714e+02, 3.52366218e+02,
-1.15560633e+01, 7.42149725e+02, 1.66477287e+02,
-1.18447102e+01, 7.36763064e+02, 1.65182437e+02,
5.41502771e+01, 2.30950820e+01, 1.74870470e+01,
-1.71258190e+01, 3.26592894e+02, 2.03220778e+02,
-1.41130065e+02, 7.04033786e+02, 3.84201614e+02,
-1.12642400e+01, 7.48636864e+02, 1.66665977e+02,
-1.14090840e+01, 7.36435064e+02, 1.63713810e+02,
1.21660845e-03, -8.60110629e-02, -1.93000576e-02,
1.57141460e+01, -2.34399996e+02, -2.86722926e+01,
5.28252697e+01, -4.40469167e+02, -1.11653705e+02,
1.03085631e+02, -5.01280352e+02, -1.93111585e+02,
7.16011844e+01, -5.88214725e+02, -2.18615940e+02,
5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
7.71909591e+01, -3.88738749e+02, -6.29099586e+01,
-2.99970496e+01, -2.25985794e+02, 6.41590789e+01,
1.03847001e+02, -7.32419021e+01, 1.04802558e+02,
1.26585822e+00, -1.20170579e+02, -2.82526049e+01,
1.57900698e+00, -1.51780249e+02, -3.52080548e+01,
8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
8.01141013e+00, -4.16078607e+02, -1.25355227e+02,
1.17740492e+00, -2.55151916e+02, -7.20503620e+01,
-1.73992688e+01, -2.44854505e+02, -9.25408725e+01,
8.70569014e-01, -1.68664569e+02, -3.73902498e+01,
1.39982512e+00, -2.00884252e+02, -4.47207875e+01,
5.24591115e-01, -1.65867774e+02, -3.68342864e+01,
5.24591115e-01, -1.65867774e+02, -3.68342864e+01]])]
I had planned to extract the first column and create a vector x extract second column vector y and third column vector z. Howver when I converted it into an array , it resulted into 1x1x96array type with the following structure
[[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 -5.41511240e+01
-2.30953821e+01 -1.74871288e+01 -4.91000564e+01 2.44382720e+02
1.71213983e+02 -1.54755310e+02 5.97881714e+02 3.52366218e+02
-1.15560633e+01 7.42149725e+02 1.66477287e+02 -1.18447102e+01
7.36763064e+02 1.65182437e+02 5.41502771e+01 2.30950820e+01
1.74870470e+01 -1.71258190e+01 3.26592894e+02 2.03220778e+02
-1.41130065e+02 7.04033786e+02 3.84201614e+02 -1.12642400e+01
7.48636864e+02 1.66665977e+02 -1.14090840e+01 7.36435064e+02
1.63713810e+02 1.21660845e-03 -8.60110629e-02 -1.93000576e-02
1.57141460e+01 -2.34399996e+02 -2.86722926e+01 5.28252697e+01
-4.40469167e+02 -1.11653705e+02 1.03085631e+02 -5.01280352e+02
-1.93111585e+02 7.16011844e+01 -5.88214725e+02 -2.18615940e+02
5.67537804e+00 -4.35088906e+02 -9.76974016e+01 7.71909591e+01
-3.88738749e+02 -6.29099586e+01 -2.99970496e+01 -2.25985794e+02
6.41590789e+01 1.03847001e+02 -7.32419021e+01 1.04802558e+02
1.26585822e+00 -1.20170579e+02 -2.82526049e+01 1.57900698e+00
-1.51780249e+02 -3.52080548e+01 8.84543993e-01 -1.07795356e+02
-2.56307189e+01 8.84543993e-01 -1.07795356e+02 -2.56307189e+01
5.67537804e+00 -4.35088906e+02 -9.76974016e+01 8.01141013e+00
-4.16078607e+02 -1.25355227e+02 1.17740492e+00 -2.55151916e+02
-7.20503620e+01 -1.73992688e+01 -2.44854505e+02 -9.25408725e+01
8.70569014e-01 -1.68664569e+02 -3.73902498e+01 1.39982512e+00
-2.00884252e+02 -4.47207875e+01 5.24591115e-01 -1.65867774e+02
-3.68342864e+01 5.24591115e-01 -1.65867774e+02 -3.68342864e+01]]]
It leads to , an array [x0,y0,z0,x1,y1,z1,x3,y3,z3...... x31,y31,z31] and i want to extract the following ,
x = [x0, x2, .......... x31]
y= [y0, y1, ...........y31]
z= [z0,z1,..............z31]
I was wondering if thats possible to do it in the list or modifying array
IICU, given an array
[x0,y0,z0,x1,y1,z1,x2,y2,z2, .... , x31,y31,z31]
You can just use numpy indexing
x = arr[np.arange(0,len(arr),3)]
y = arr[np.arange(1,len(arr),3)]
z = arr[np.arange(2,len(arr),3)]
Edit:
As suggested in subsampling every nth entry in a numpy array
, Numpy.ndarray offers a simple and elegant option:
import numpy as np
arr = np.array(my_list)
x = arr[::3]
y = arr[1::3]
z = arr[2::3]
Original answer:
I'm not sure about the structure of your input list, as what you provided here is an invalid data.
Despite the extra brackets in your source data, this can be done with a fairly simple trick, and you don't have to be bothered with Numpy:
Assuming that you are trying to somehow split the list into three vectors in your specified way, assign your list to a, then you could have your desired lists by:
x = [a[3*i] for i in range(int(len(a)/3))]
y = [a[3*i+1] for i in range(int(len(a)/3))]
z = [a[3*i+2] for i in range(int(len(a)/3))]
My code
import numpy as np
from numpy import loadtxt
s = loadtxt("sest.txt", delimiter=" ", unpack=False)
b = loadtxt("base.txt", delimiter=" ", unpack=False)
d=b-s
e = np.absolute(d)
me = e.argsort()[-100:][::-1]
print me
I got
[400600 401600 399600 400601 401601 399601 401599 400599 399599 399602
401602 400602 399598 401598 400598 400603 401603 399603 401597 399597
401604 400597 399604 400604 400605 399605 401605 401596 399596 400596
399606 401606 400606 399595 401595 400595 399607 401607 400607 400608
400594 401608 399608 401594 399594 400609 401609 399609 401593 400593
399593 401610 400610 399610 400592 401592 399592 399611 400611 401611
399591 401612 401591 400612 400591 399612 399613 401613 400613 399590
400590 401590 400614 399614 401614 399589 400589 401589 401615 399615
400615 401616 399616 400616 400588 399588 401588 400617 401617 399617
401587 400587 399587 400618 399618 401618 399586 400586 401586 400619]
Works fine.But I want to specify all elements in d that are larger then 2.5?So I do not care if there are 100 or 200 just everything above this threshold level.Is it possible to extend argsort or not?
if you are just seeking array elements above a certain threshold value, you can use x[x>a], where a is the threshold. For purposes of illustration, I will show now using ipython and edit later. Let us assume "x" is some numpy array:
In [9]: x=np.random.rand(1,10) # an array with random elements
In [10]: print x[x>0.6] # select elements above 0.6
[ 0.71733906 0.74028607 0.66293195 0.86649922 0.7423685 0.71807904
0.8215429 ]
In [11]: print x
[[ 0.36655557 0.71733906 0.74028607 0.66293195 0.86649922 0.21478604
0.7423685 0.71807904 0.30482062 0.8215429 ]]