Re: default value in a list

2005-01-24 Thread TB
Thanks very much for all the responses. They were useful to me and I'll probably refer back to them again in the future. TB -- http://mail.python.org/mailman/listinfo/python-list

Re: default value in a list

2005-01-22 Thread Reinhold Birkenfeld
Nick Coghlan wrote: > Reinhold Birkenfeld wrote: >>>Why not put these together and put it in itertools, since the requirement >>>seems >>>to crop up every other week? >>> >>> >>> line = "A:B:C".split(":") >>> ... >>> >>> def ipad(N,iterable, default = None): >>> ... return it.islice(it.ch

Re: default value in a list

2005-01-22 Thread Nick Coghlan
Reinhold Birkenfeld wrote: Why not put these together and put it in itertools, since the requirement seems to crop up every other week? >>> line = "A:B:C".split(":") ... >>> def ipad(N,iterable, default = None): ... return it.islice(it.chain(iterable, it.repeat(default)), N) ... >>> a,b

Re: default value in a list

2005-01-22 Thread Reinhold Birkenfeld
Michael Spencer wrote: > Alex Martelli wrote: > [explanation and the following code:] > >> >>> a, b, c = it.islice( >> ... it.chain( >> ... line.split(':'), >> ... it.repeat(some_default), >> ... ), >> ... 3) >> ...

Re: default value in a list

2005-01-22 Thread Michael Spencer
Alex Martelli wrote: [explanation and the following code:] >>> a, b, c = it.islice( ... it.chain( ... line.split(':'), ... it.repeat(some_default), ... ), ... 3) ... ... >>> def pad_with_default(N, iter

Re: default value in a list

2005-01-22 Thread Nick Craig-Wood
Alex Martelli <[EMAIL PROTECTED]> wrote: > Nick Craig-Wood <[EMAIL PROTECTED]> wrote: > ... > > Or this version if you want something other than "" as the default > > > > a, b, b = (line.split(':') + 3*[None])[:3] > > Either you mean a, b, c -- or you're being subtler than I'm > grasping

Re: default value in a list

2005-01-22 Thread Bengt Richter
On Fri, 21 Jan 2005 17:04:11 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote: >TB wrote: > >> Hi, >> >> Is there an elegant way to assign to a list from a list of unknown >> size? For example, how could you do something like: >> >> > a, b, c = (line.split(':')) >> >> if line could have less

Re: default value in a list

2005-01-22 Thread Alex Martelli
Nick Craig-Wood <[EMAIL PROTECTED]> wrote: ... > Or this version if you want something other than "" as the default > > a, b, b = (line.split(':') + 3*[None])[:3] Either you mean a, b, c -- or you're being subtler than I'm grasping. > BTW This is a feature I miss from perl... Hmmm, I unde

Re: default value in a list

2005-01-22 Thread Nick Craig-Wood
TB <[EMAIL PROTECTED]> wrote: > Is there an elegant way to assign to a list from a list of unknown > size? For example, how could you do something like: > > >>> a, b, c = (line.split(':')) > if line could have less than three fields? You could use this old trick... a, b, c = (line+"::").s

Re: default value in a list

2005-01-22 Thread Peter Otten
Peter Otten wrote: > Paul McGuire wrote: > >>> Is there an elegant way to assign to a list from a list of unknown >>> size? For example, how could you do something like: >>> >>> >>> a, b, c = (line.split(':')) >>> if line could have less than three fields? > >> I asked a very similar question

Re: default value in a list

2005-01-22 Thread Peter Otten
Paul McGuire wrote: >> Is there an elegant way to assign to a list from a list of unknown >> size? For example, how could you do something like: >> >> >>> a, b, c = (line.split(':')) >> if line could have less than three fields? > I asked a very similar question a few weeks ago, and from the va

Re: default value in a list

2005-01-22 Thread Alex Martelli
TB <[EMAIL PROTECTED]> wrote: > Is there an elegant way to assign to a list from a list of unknown > size? For example, how could you do something like: > > >>> a, b, c = (line.split(':')) > if line could have less than three fields? import itertools as it a, b, c = it.islice( i

Re: default value in a list

2005-01-21 Thread Fredrik Lundh
Paul McGuire wrote: > I asked a very similar question a few weeks ago, and from the various > suggestions, I came up with this: > > expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen] I wouldn't trust whoever suggested that. if you want a function, use a function: def e

Re: default value in a list

2005-01-21 Thread Jeff Shannon
TB wrote: Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? (Note that you're actually assigning to a group of local variables, via tuple unpacking, not

Re: default value in a list

2005-01-21 Thread Steven Bethard
Paul McGuire wrote: expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen] Or if you're afraid of lambda like me: def expand(lst,default,minlen):return (lst + [default]*minlen)[0:minlen] or perhaps more readably: def expand(lst, default, minlen): return (lst + [default]*minlen)

Re: default value in a list

2005-01-21 Thread Paul McGuire
"TB" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > Is there an elegant way to assign to a list from a list of unknown > size? For example, how could you do something like: > > >>> a, b, c = (line.split(':')) > if line could have less than three fields? > > Thanks, > TB > I

Re: default value in a list

2005-01-21 Thread Steve Holden
TB wrote: Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? l = line.split(':') l is a list, whose length will be one more than the number of colons in

Re: default value in a list

2005-01-21 Thread Larry Bates
What do you want put into the "missing" variables? I'll assume None. Something like following works: values=line.split(':') try: a=values.pop(0) except IndexError: a=None try: b=values.pop(0) except IndexError: b=None try: c=values.pop(0) except IndexError: c=None Larry Bates TB wrote: Hi, Is the

default value in a list

2005-01-21 Thread TB
Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: >>> a, b, c = (line.split(':')) if line could have less than three fields? Thanks, TB -- http://mail.python.org/mailman/listinfo/python-list