[Python-ideas] zip_strict() or similar in itertools ?
Alfredo Solano MartÃnez
asolano at icai.es
Thu Apr 4 14:35:21 CEST 2013
> Sure, that would be the alternative, but it's not a very general solution
> since you would have to figure out a fill marker that can never be part of
> the specific iterable.
> What's worse is that you're retrieving several
> elements per iteration, and those different elements may have different
> properties requiring different markers. For example, in a file every first
> line might be an arbitrary string, every second a number, every third could
> optionally be blank, and so on. So I guess, catching the problem early and
> raising an error right then, is a simpler and clearer solution.
>
> Wolfgang
Indeed, the question is still open. I was talking about the speed penalty of
your interim solution. About the selection of a marker, what about a
custom class?
# None of your data will be this
class Marker(): pass
# Same as the docs recipes
def grouper(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
# And then do something like
for t in grouper(3, 'ABCDEFG', Marker):
if Marker in t: print('Marker) # or raise ValueError, ...
Alfredo
More information about the Python-ideas
mailing list