
This might be a silly idea, but I was wondering about forcing split() to return at least X number of items. For example, I might be getting a comma separated string and i want to split it up into names, but I might have less than all of them. If it is just 0, 1, or 2, I can use partition(), but any more and that doesn't work. Besides, I don't care if the separator is there, just to get the values. Might also make sense to give the values to give by default. Example of implementing this: def split(self, sep=None, max_splits=None, min_items=None): parts = self.split(sep, max_splits) if len(parts) < min_items: parts.extend([None] * (min_items - len(parts))) return parts Use would be like this: a, b, c, d = "1,2,3".split(',', None, 4) Probably not a great idea, but I'm tossing it out there, anyway. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://techblog.ironfroggy.com/ Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

----- Mensaje original ----
How about this? def splitter(string,sep=None,max_splits=-1, min_items=None): parts = string.split(sep,max_splits) for part in parts: yield part for i in range(min_items-len(parts)): yield a, b, c, d = splitter("1,2,3", ",", -1, 4) Vitor ¡Todo sobre la Liga Mexicana de fútbol! Estadisticas, resultados, calendario, fotos y más:< http://espanol.sports.yahoo.com/

On Tue, Jan 20, 2009 at 5:32 PM, Calvin Spealman <ironfroggy@gmail.com> wrote:
Doesn't strike me as easy to use; the None values returned would most likely cause a traceback a few lines down. Returning '' might be better, but I still think it's very marginal functionality and makes the API ugly due to the large number of optional arguments. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

Calvin Spealman wrote:
This is not a common thing to do, and it is easy enough to implement in a single assignment even without a helper function: a, b, c, d = ("1,2,3".split(',', 4) + [None] * 4)[:4] This also has the advantage of giving you explicit control of the filler value for missing pieces. (Note: be careful not to use a _mutable_ filler value in this manner!) - Tal

list(fixlen([1, 2, 3], 5))=0A> > [1, 2, 3, None, None]=0A> >=0A> =0A> I=
MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable ----- Mensaje original ----=0A> De: Mathias Panzenb=F6ck <grosser.meister.m= orti@gmx.net>=0A> Para: Arnaud Delobelle <arnodel@googlemail.com>=0A> CC: p= ython-ideas@python.org=0A> Enviado: mi=E9rcoles, 21 de enero, 2009 10:55:08= =0A> Asunto: Re: [Python-ideas] min_fields argument to str.split()=0A> =0A>= Arnaud Delobelle schrieb:=0A> >=0A> > How about this?=0A> >=0A> >>>> from = itertools import *=0A> >>>> def fixlen(iterable, len, item=3DNone):=0A> > .= ..=A0 =A0 return islice(chain(iterable, repeat(item)), len)=0A> > ...=0A> >= like this. Maybe an addition to itertools?=0A> =0A> +1=0A=0A+1 from me too= .=A0Way better than the generator I proposed. Having this as a function als= o makes it=A0applicable to much more than just one use case.=0A=0AIf you se= e the following code:=0A=0Aa,b,c,d =3D fixlen(str.split(),4)=0A=0Ayou immed= iately know it's not a regular unpacking operation; it will be the first pl= ace you look if something goes wrong.=0A=0A=0AVitor=0A=0A=0A =A1Todo s= obre la Liga Mexicana de f=FAtbol! Estadisticas, resultados, calendario, fo= tos y m=E1s:<=0Ahttp://espanol.sports.yahoo.com/

On Wed, 21 Jan 2009 14:55:08 +0100, Mathias Panzenböck wrote:
I opened an issue_ at the Python bug tracker. .. _issue: http://bugs.python.org/issue5034 -- Robert "Stargaming" Lehmann

----- Mensaje original ----
How about this? def splitter(string,sep=None,max_splits=-1, min_items=None): parts = string.split(sep,max_splits) for part in parts: yield part for i in range(min_items-len(parts)): yield a, b, c, d = splitter("1,2,3", ",", -1, 4) Vitor ¡Todo sobre la Liga Mexicana de fútbol! Estadisticas, resultados, calendario, fotos y más:< http://espanol.sports.yahoo.com/

On Tue, Jan 20, 2009 at 5:32 PM, Calvin Spealman <ironfroggy@gmail.com> wrote:
Doesn't strike me as easy to use; the None values returned would most likely cause a traceback a few lines down. Returning '' might be better, but I still think it's very marginal functionality and makes the API ugly due to the large number of optional arguments. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

Calvin Spealman wrote:
This is not a common thing to do, and it is easy enough to implement in a single assignment even without a helper function: a, b, c, d = ("1,2,3".split(',', 4) + [None] * 4)[:4] This also has the advantage of giving you explicit control of the filler value for missing pieces. (Note: be careful not to use a _mutable_ filler value in this manner!) - Tal

list(fixlen([1, 2, 3], 5))=0A> > [1, 2, 3, None, None]=0A> >=0A> =0A> I=
MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable ----- Mensaje original ----=0A> De: Mathias Panzenb=F6ck <grosser.meister.m= orti@gmx.net>=0A> Para: Arnaud Delobelle <arnodel@googlemail.com>=0A> CC: p= ython-ideas@python.org=0A> Enviado: mi=E9rcoles, 21 de enero, 2009 10:55:08= =0A> Asunto: Re: [Python-ideas] min_fields argument to str.split()=0A> =0A>= Arnaud Delobelle schrieb:=0A> >=0A> > How about this?=0A> >=0A> >>>> from = itertools import *=0A> >>>> def fixlen(iterable, len, item=3DNone):=0A> > .= ..=A0 =A0 return islice(chain(iterable, repeat(item)), len)=0A> > ...=0A> >= like this. Maybe an addition to itertools?=0A> =0A> +1=0A=0A+1 from me too= .=A0Way better than the generator I proposed. Having this as a function als= o makes it=A0applicable to much more than just one use case.=0A=0AIf you se= e the following code:=0A=0Aa,b,c,d =3D fixlen(str.split(),4)=0A=0Ayou immed= iately know it's not a regular unpacking operation; it will be the first pl= ace you look if something goes wrong.=0A=0A=0AVitor=0A=0A=0A =A1Todo s= obre la Liga Mexicana de f=FAtbol! Estadisticas, resultados, calendario, fo= tos y m=E1s:<=0Ahttp://espanol.sports.yahoo.com/

On Wed, 21 Jan 2009 14:55:08 +0100, Mathias Panzenböck wrote:
I opened an issue_ at the Python bug tracker. .. _issue: http://bugs.python.org/issue5034 -- Robert "Stargaming" Lehmann
participants (7)
-
Arnaud Delobelle
-
Calvin Spealman
-
Guido van Rossum
-
Mathias Panzenböck
-
Robert Lehmann
-
Tal Einat
-
Vitor Bosshard