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'

  • 326 Views

FAQ

Years=[1994,1891,2010,1999,1700,1698,2004] code t…

count=0
years=[1994,1891,2010,1999,1700,1698,2004]
for i in years:
   if(i%4==0 and i%100!=0 o…

ImportError: DLL load failed while importing _cex…

To solve this error you can use . This error came when i worked on matplotlib in python. This is th…

Program to swap variables in python

A program in Python that allows two variables to swap values.

def swap(x, y):

   temp = x
…