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
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
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
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)
>> ...
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
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
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
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
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
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
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
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
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
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
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)
"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
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
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
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
19 matches
Mail list logo