index()-like behavior that returns all occurences...
Paul Jackson
pj at sgi.com
Wed Dec 19 17:39:11 EST 2001
|> x = [ [1], [1,2], [], [1,2,3], [], [2], [12] ]
|>
|> I want to find the index of all of the empty lists. In other words, I'd
|> like a function foo that would behave like this:
|>
|> >> foo(x)
|> [2 4]
Well, you could do a one liner with the lambda, map, zip, filter routines:
map (lambda x: x[0], filter (lambda x: not x[1], zip (range(len(x)), x)))
But this has _got_ to be less readable than the simple for loop,
even to whomever it was that championed getting map, reduce and
filter into the Python language in the first place.
--
I won't rest till it's the best ...
Manager, Linux Scalability
Paul Jackson <pj at sgi.com> 1.650.933.1373
More information about the Python-list
mailing list