i have made a function which uses subtraction of two values stored in two lists as follows:
import sys,os
import math
c1 = [10]
c2 = [5]
d1 = [8]
d2 = [4]
x = d2 - c2
y = d1 - c1
z = x*x
w = y*y
answer = sqrt(z + w)
print answer
My error is: TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
How can I get over the error that occurs due to subtraction not being possible between two lists, i.e., in lines d2-d1 and c2-c1? Is there a built-in function in the math module similar to sqrt that i might use to subtract lists?
You’re using one element lists; If you want to perform that calculation specifically, just remove the braces. I’ll assume that you actually do have multi-valued lists. A reasonable solution is to combine
map(), which applies a function to each element in one or more lists, as well as some of the functions from theoperatormodule, which turn many python operators (like+and-) into functions.First well just set up some lists.
Next we just replace each of your operations into a
mapcall to the correspondingoperator.*