[Python-ideas] min_fields argument to str.split()
Vitor Bosshard
algorias at gmail.com
Thu Jan 22 02:58:06 CET 2009
NOTE: It was brought to my attention that my last e-mail message arrived
completely garbled. Here it is again, as I meant to send it. My Apologies to
everyone.
> >
> > How about this?
> >
> >>>> from itertools import *
> >>>> def fixlen(iterable, len, item=None):
> > ... return islice(chain(iterable, repeat(item)), len)
> > ...
> >>>> list(fixlen([1, 2, 3], 5))
> > [1, 2, 3, None, None]
> >
>
> I like this. Maybe an addition to itertools?
>
> +1
+1 from me too.
Way better than the generator I proposed. Having this as a function also
makes it applicable to much more than just one use case.
If you see the following code:
a,b,c,d = fixlen(str.split(),4)
You immediately know it's not a regular unpacking operation; it will be the
first place you look if something goes wrong.
Vitor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090121/e1fb3ef0/attachment.html>
More information about the Python-ideas
mailing list