0

I have a list in python of probabilites of something

aprob=[ 0.2 ,0.3, 0.0, 0.1, 0.4]

and I want to get a list with the probabilities of the opposite event

a_not_prob= [0.8, 0.7,1.0, 0.9,0.6]

The thing is that I am watching a tutorial and there the following works, but for me it throws an error:

a_not_prob=1.0-aprob

For me, it throws

 unsupported operand type(s) for -: 'float' and 'list'

The funny thing is that for the instructor it works...

so How can I get the second list from the first one?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

2 Answers2

1

you can't do subtraction between list and float you can do something like that:

aprob=[ 0.2 ,0.3, 0.0, 0.1, 0.4]
ans = [1 - prob for prob in aprob]
print(ans)
TanjiroLL
  • 1,354
  • 1
  • 5
  • 5
0

The syntax you had reminds me of MATLAB. I haven't seen anything like that in python. But you can simply use "list comprehension" to accomplish what you're looking for. Here you are:

a_not_prob = [ 1-x for x in aprob]

Vince Payandeh
  • 383
  • 2
  • 7
  • i wonder why people put -1 to an answer without explaning. I updated your answer – KansaiRobot Dec 05 '20 at 05:32
  • maybe cause the statement "I haven't seen anything like that in python" is false. because if it was a numpy array that is perfectly valid syntax – Derek Eden Dec 09 '20 at 03:11
  • @DerekEden I sure could be wrong, but my statement is still True. "I" haven't seen anything in python like that yet, but by no means I claim to know everything about python and all the libraries out there. I really meant "native python" syntax like that is something I haven't seen. anyway, thank you:) – Vince Payandeh Dec 10 '20 at 00:18