Home > Blockchain >  Unpacking iteratively a tuple? A tuple of floats I'd like to pass to 2 params in a function
Unpacking iteratively a tuple? A tuple of floats I'd like to pass to 2 params in a function

Time:10-09

import numpy as np

x = ['0.01107', '0.02314', '0.03321', '0.04428', '0.08035']
y = ['0.8864', '0.6703', '0.4542', '0.3382', '0.2321']
hypotenuse_array = np.hypot(x, y)
print("Hypotenuse_array = ", hypotenuse_array)

Doesn't work because of float I think?

a_zip = zip(x, y)
zipped = list(a_zip)
print(zipped)

How to pass X & Y zipped list to np.hypot on loop?

CodePudding user response:

I think the first issue is that you are using strings in the arrays of x and y. If you try to use numbers whith the following code, it will produce a result. But I am not sure if that is what you want to achieve though.

import numpy as np

x = [0.01107, 0.02314, 0.03321, 0.04428, 0.08035]
y = [0.8864, 0.6703, 0.4542, 0.3382, 0.2321]
hypotenuse_array = np.hypot(x, y)
print("Hypotenuse_array = ", hypotenuse_array)

CodePudding user response:

Convert string arrays to float.

import numpy as np

x = ['0.01107', '0.02314', '0.03321', '0.04428', '0.08035']
y = ['0.8864', '0.6703', '0.4542', '0.3382', '0.2321']

# convert x & y to numpy float as we pass them to hypot
hypotenuse_array = np.hypot(np.array(x).astype(np.float), np.array(y).astype(np.float))

print("Hypotenuse_array = ", hypotenuse_array)
# Output: array([0.88646912, 0.6706993 , 0.4554125 , 0.34108644, 0.2456146 ])
  • Related