Instagram
youtube
Facebook

Q) TypeError: bad operand type for unary -: 'str'

Answer:


Edit 1: As requested, here is the rest of the code to the function.
with open(file_path, 'r') as f:
result = [tuple(map(int, line.split())) for line in f]

for i in range(len(result)):

   for j in range(len(result[i])):
      result[i][j] = (result[i][j] - min(result[i])) / (max(result[i]) - min(result[i]))
      result[i] = np.array(result[i])

print(result)

I have edited the code to use int() but it still doesn't seem to work.
Edit 2: It works now. Thank you everyone for helping me!

A:

int('0.0') raises a TypeError because int() cannot convert strings that do not contain only whole numbers (without decimals). float() can convert these strings to floats.
int('1.0')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'float'

  • 492 Views

FAQ

High frequency noise at solving differential equa…

High Frequency Noise in Solving Differential Equations

When solving differential equations, high…

Sms code not coming from pyrogram for new accounts

Troubleshooting SMS Code Not Coming from Pyrogram for New Accounts

Pyrogram is a popular Python …

How to use solve_ivp solve Partial Differential E…

Solving Partial Differential Equations with Spectral Methods using `solve_ivp`

The `solve_ivp` f…