0

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
1
  • 2
    The 1d object dtype array is effectively a list of lists. If the lists are all the same length you could use np.stack(var) (or vstack) to make a 2d array. Otherwise you'll need to use a list comprehension to iterate through the lists, and select the desired value. Commented Dec 10, 2022 at 17:11

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

Comments

0

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)

3 Comments

J don't think yiu var duplicates his. His is 1d, your's 2d
Hello Dima indeed in your example var is 2d. And when I print your var with print(var) it is different as given in my question: [[9166855000000.0 13353516.0] [7818836000000.0 11389833.0] [20269756000000.0 29527304.0] [66886956000000.0 97435384.0] [58686560000000.0 85489730.0] [50809440000000.0 74014984.0]]. print(np.shape(var)) is for your var (6, 2) and in my case I get print(np.shape(var)) (6,).
@len could you provide an example that generates a structure of shape (6,) as you are having?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.