mardi 19 février 2019

Multiple conditional statements for list comprehension Python

I have a list with elements that I want to do operations on with 2 conditions.

One condition is to multiply a norm function by -1 if the element is negative or leaving the norm function positive if the element is positive.

The list of values looks like this:

print(values)

-97476.70633454417
-93170.30642401175
-89901.82679086612
-87187.62533194348
-87269.09594982903
-85513.31676486236
-83545.26529198853
-82411.91255123452
-81620.01849452594

As you can see they are all negative (in this experiment).

The code looks like this:

norm_BIC = [(-1.0) * float(i)/max(values) for i in values if i < 0 or float(i)/max(values) for i in values if i > 0]

If I run the code before the or statement it works:

norm_BIC = [(-1.0) * float(i)/max(values) for i in values if i < 0]

Which means it's everything else following that doesn't work because I get an empty list for norm_BIC when running after the or.

How do I fix this condition?

Aucun commentaire:

Enregistrer un commentaire