question about a program

Richard Thomas chardster at gmail.com
Fri Oct 8 16:00:10 EDT 2010


On Oct 8, 1:39 am, Logan Butler <killable1... at gmail.com> wrote:
> question about an assignment:
>
> >>> places("home sweet home is here",' ')
>
> [4, 10, 15, 18]
>
> this is my code:
>
> def places(x, y):
>     return [x.index(y) for v in x if (v == y)]
>
> so far I'm only getting
> [4, 4, 4, 4]
>
> so the first value is correct, it is just not iterating on to the next
> three items it needs to

Try this instead:

def places(x, y):
    return [i for i, v in enumerate(x) if v == y]



More information about the Python-list mailing list