On Fri, 8 Oct 2010 14:34:21 -0700 Chris Rebert <c...@rebertia.com> wrote:
> On Thu, Oct 7, 2010 at 5:39 PM, Logan Butler <killable1...@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 > > Your loop variable is v, but your expression `x.index(y)` does not use > v at all, and hence its value is invariant over the course of the > loop. > Nice catch, but the "if (v==y)" clause nullifies that in any case. The real reason is that list.index(x) returns the index of the *first* occurrence of x, so its invariant by default. /W -- To reach me via email, replace INVALID with the country code of my home country. But if you spam me, I'll be one sour Kraut. -- http://mail.python.org/mailman/listinfo/python-list