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

How do I count the last elements in a list after a certain element?

I have a python list containing zeros and ones like this:

a = [1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0]

I know how to count the ones and zeros in this, but what I can’t manage to figure out is how to count the last zeros after the last 1.0 in that list. In this case the solution would be "2".
I would like to have a simple code which I can use for this problem in order to put it in a loop.

I hope someone can help me with that. Thank you!

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

>Solution :

Try this:

a = [1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
a[::-1].index(1)

You reverse the list, and take the index of the first 1.

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