Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

iterate through list of lists and check if list contains all "False"

I am having issue with my code that works fine on sample code and should also work on test code but it doesn’t.
I am having a list of lists that contain True/False but as STRING! List looks like this:

['True', 'True', 'True']
['False', 'False', 'False']
['False', 'False', 'False']
['False', 'False', 'False']
['True', 'True', 'True']
['True', 'True', 'False']
['True', 'True', 'True']
['True', 'True', 'False']
['True', 'True', 'True']

now I want to print OK if all last three elements were "True", SHUT_DOWN when last three elements were "False" and WARNING when last element was "False".
Result would look like this:

['True', 'True', 'True'] ==> OK
['False', 'False', 'False'] ==> SHUT_DOWN
['False', 'False', 'False'] ==> SHUT_DOWN
['False', 'False', 'False'] ==> SHUT_DOWN
['True', 'True', 'True'] ==> OK
['True', 'True', 'False'] ==> WARNING
['True', 'True', 'True'] ==> OK
['True', 'True', 'False'] ==> WARNING
['True', 'True', 'True'] ==> OK

this is my code that works on sample code but not on test code. On test code I get WARNING instead of SHUT_DOWN.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  for p in path:
    if all(item is "False" for item in p[-3:]):
        print("SHUT_DOWN")
    elif p[-1] == "False":
        print("WARNING")
    else:
        print("OK")

what I am doing wrong and getting two different results? Is there maybe a better way of doing this type of operation? Maybe convert "True" and "False" to boolean instead of string? Thx!

>Solution :

If you’re loading your data from somewhere external, is may not do what you expect, since it compares object identity, not contents. (You should never use is unless you know exactly what you’re doing).

This seems to work fine:

for case in [
    ['True', 'True', 'True'],
    ['False', 'False', 'False'],
    ['False', 'False', 'False'],
    ['False', 'False', 'False'],
    ['True', 'True', 'True'],
    ['True', 'True', 'False'],
    ['True', 'True', 'True'],
    ['True', 'True', 'False'],
    ['True', 'True', 'True'],
]:
    if all(x == "False" for x in case[-3:]):
        result = "SHUT_DOWN"
    elif case[-1] == "False":
        result = "WARNING"
    else:
        result = "OK"
    print(case, "==>", result)
['True', 'True', 'True'] ==> OK
['False', 'False', 'False'] ==> SHUT_DOWN
['False', 'False', 'False'] ==> SHUT_DOWN
['False', 'False', 'False'] ==> SHUT_DOWN
['True', 'True', 'True'] ==> OK
['True', 'True', 'False'] ==> WARNING
['True', 'True', 'True'] ==> OK
['True', 'True', 'False'] ==> WARNING
['True', 'True', 'True'] ==> OK
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading