Home > OS >  If condition on multiple list
If condition on multiple list

Time:05-17

Am trying to write an if condition in which I test on the standard deviation of several lists that are in a dictionary, The if statement that I did didn't work for me as you can see in the code :

a= {}

a[0] = [0.9907568097114563, 0.9913344979286194, 0.9907568097114563, 0.9913344979286194, 
0.9907568097114563]
a[1] = [0.8538417220115662, 0.8526862859725952, 0.8411322832107544, 0.8659734129905701, 
0.851530909538269]
a[n] = ...

if np.std(a[i] for i in range(len(a)) < 0.01 :
   print("Early stopping here !")

CodePudding user response:

I am not sure what you're trying to do, but you are calling an np.std on a generator "a[i] for i in range(len(a))", you have a syntax error (missing ')') and are using a dictionary like a list (which although might work, I don't recommend it). Consider using "any" for the if statement, you're code would look something like this:

import numpy as np
a= {}

a[0] = [0.9907568097114563, 0.9913344979286194, 0.9907568097114563, 0.9913344979286194, 
0.9907568097114563]
a[1] = [0.8538417220115662, 0.8526862859725952, 0.8411322832107544, 0.8659734129905701, 
0.851530909538269]
# a[n] = ...
if all([np.std(a[i]) < 0.01 for i in a.keys()]):
    print("Early stopping here !")

CodePudding user response:

if tests for… well, a condition—truthfulness of one expression.

You can get what you want in a few different ways—you can create a condition that checks multiple things (for which any() may or may not be useful), or maybe a loop with the single checks inside would be more helpful.

  • Related