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
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(
it.chain(
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 various
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 a few weeks ago, and
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 =
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 understand
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 than three fields?
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.
Just a typo -
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, iterable,
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)
...
...
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,c,d =
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.chain(iterable, it.repeat(default)),
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
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 asked a very
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 +
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,
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
17 matches
Mail list logo