vendredi 9 septembre 2016

Finding indices from list comprehension in Python 3

I want to find the indices of matches in a list using the best Python syntax. Here is what I have:

v = ['=', 'c', '=', 'c', 'c', 'c', '=', 'c', 'c', '=']

Now, return the list of integers for the condition of:

'=' in v is True

So far, I have:

[v.index(i) for i in v if i=='=']

which return:

[ 0, 0, 0, 0 ]

instead of:

[ 0, 2, 6, 9 ]

I'm missing the last step and I don't want to put it into a 'for' loop'.

Aucun commentaire:

Enregistrer un commentaire