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 need to compare two list of vectors and take their equal elements, like:
veclist1 = [(0.453 , 0.232 , 0.870), (0.757 , 0.345 , 0.212), (0.989 , 0.232 , 0.543)]
veclist2 = [(0.464 , 0.578 , 0.870), (0.327 , 0.335 , 0.562), (0.757 , 0.345 , 0.212)]
equalelements = [(0.757 , 0.345 , 0.212)]
obs: The order of the elements don't matter!
And also, if possible I wanted to only consider till the 2nd decimal in the comparison
but without rounding or shortening them. Is it possible ?
Thx in advance!
# Get new lists with rounded values
veclist1_rounded = [tuple(round(val, 2) for val in vec) for vec in veclist1]
veclist2_rounded = [tuple(round(val, 2) for val in vec) for vec in veclist2]
# Convert to sets and calculate intersection (&)
slct_rounded = set(veclist1_rounded) & set(veclist2_rounded)
# Pick original elements from veclist1:
# - get index of the element from the rounded list
# - get original element from the original list
equalelements = [veclist1[veclist1_rounded.index(el)] for el in slct_rounded]
In this case we select the entries of veclist1 if only the rounded entries are equal. Otherwise the last line needs to be adjusted.
If all original elements are needed, the final list can be calculated using both original lists:
equalelements = ([veclist1[veclist1_rounded.index(el)] for el in slct_rounded]
+ [veclist2[veclist2_rounded.index(el)] for el in slct_rounded])
Note: round might have issues, which should be solved in current Python version. Nevertheless, it might be better to use strings instead:
get_rounded = lambda veclist: [tuple(f'{val:.2f}' for val in vec) for vec in veclist]
veclist1_rounded, veclist2_rounded = get_rounded(veclist1), get_rounded(veclist2)
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))]
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)
afile is a given file, and z the degree of the polynomial. Been breaking my head over this for a while, frustrating how I'm basically given zero instructions on how to proceed.
This is what I thought it should be like:
import numpy as np
def newfile(afile,z):
x,y = np.loadtxt(afile)
d= np.polyfit(x,y,z)
return d
I have attempted to do it as
data = np.loadtxt(afile)
x = data[0:]
by printing "data" I'm given this format:
[[ 2. 888.8425]
[ 6. 888.975 ]
[ 14. 888.1026]
[ 17. 888.2071]
[ 23. 886.0479]
[ 26. 883.3316]
[ 48. 877.04 ]
[ 99. 854.3665]]
By printing "x" in this case just gives me the whole list (I'm thinking the issue lies in the lack of comma). In this case I'd want x to be an array of the left column.
I suppose you are getting an error when unpacking in this statement:
x,y = np.loadtxt(afile)
you should replace it for this:
x, y = zip(*np.loadtxt(afile))
the rest should work