I have a variable var
When I print it in jupyter, it gives:
var
#array([list([9166855000000.0, 13353516.0]),
# list([7818836000000.0, 11389833.0]),
# list([20269756000000.0, 29527304.0]),
# list([66886956000000.0, 97435384.0]),
# list([58686560000000.0, 85489730.0]),
# list([50809440000000.0, 74014984.0])], dtype=object)
or
print(var)
[list([9166855000000.0, 13353516.0])
list([7818836000000.0, 11389833.0])
list([20269756000000.0, 29527304.0])
list([66886956000000.0, 97435384.0])
list([58686560000000.0, 85489730.0])
list([50809440000000.0, 74014984.0])]
The type is:
print(type(var))
#<class 'numpy.ndarray'>
How can I devide the second elements of the sublists by the first ones?
I want to get the following values as an array or list:
13353516.0/9166855000000.0
...
74014984.0/50809440000000.0
CodePudding user response:
The solution seems to be rather straightforward
var[:,1] / var[:,0]
would output
array([1.4567172710815215e-06, 1.4567172146851526e-06,
1.4567172885554222e-06, 1.456717270853229e-06,
1.4567173472086283e-06, 1.4567171769655403e-06], dtype=object)
To reproduce define var
as follows:
import numpy as np
var = np.array([list([9166855000000.0, 13353516.0]),
list([7818836000000.0, 11389833.0]),
list([20269756000000.0, 29527304.0]),
list([66886956000000.0, 97435384.0]),
list([58686560000000.0, 85489730.0]),
list([50809440000000.0, 74014984.0])], dtype=object)
CodePudding user response:
If I generate the structure like this:
import numpy as np
var = np.empty([6,], dtype=object)
ll = [list([9166855000000.0, 13353516.0]),
list([7818836000000.0, 11389833.0]),
list([20269756000000.0, 29527304.0]),
list([66886956000000.0, 97435384.0]),
list([58686560000000.0, 85489730.0]),
list([50809440000000.0, 74014984.0])]
for i, l in enumerate(ll):
var[i] = l
then var
contains a 1D array (of lists)
array([list([9166855000000.0, 13353516.0]),
list([7818836000000.0, 11389833.0]),
list([20269756000000.0, 29527304.0]),
list([66886956000000.0, 97435384.0]),
list([58686560000000.0, 85489730.0]),
list([50809440000000.0, 74014984.0])], dtype=object)
and the solution might be:
[var[i][1] / var[i][0] for i in range(len(var))]
returning a list:
[1.4567172710815215e-06,
1.4567172146851526e-06,
1.4567172885554222e-06,
1.456717270853229e-06,
1.4567173472086283e-06,
1.4567171769655403e-06]
Or, indeed, a more elegant solution would be using @hpaulj's suggestion and @DmitriChubarov's solution:
var = np.stack(var)
var[:,1] / var[:,0]
returning an array:
array([1.45671727e-06, 1.45671721e-06, 1.45671729e-06, 1.45671727e-06,
1.45671735e-06, 1.45671718e-06])