列表中的默认值 [英] default value in a list
问题描述
是否有一种优雅的方式从未知
大小的列表中分配列表?例如,你怎么能这样做:
a,b,c =(line.split(' ':''))
如果行可以少于三个字段?
谢谢,
TB
你想把什么放入失踪中?变量?
我会假设没有。类似关注
的作品:
values = line.split('':'')
try:a = values .pop(0)
除了IndexError之外的
:a =无
尝试:b = values.pop(0)
除了IndexError之外的
:b =没有
尝试:c = values.pop(0)
除了IndexError:c =无
拉里贝茨
>
TB写道:
是否有一种优雅的方式从未知
大小的列表中分配列表?例如,你怎么能这样做:
a,b,c =(line.split('':''' ))
如果行可以少于三个字段?
谢谢,
TB
TB写道:
是否有一种优雅的方式从列表中分配列表未知
大小?例如,你怎么能这样做:
a,b,c =(line.split('':''' ))
如果行可以少于三个字段?
l = line.split('':'')
l是一个列表,其长度将比行中的冒号数多一个。
您可以使用[a]访问列表中的元素0],a [2],依此类推。对于
示例:
line =" This:is:a:sample:line"
l = line.split('': '')
l
[''这'',''是'','''',''''''''''''''',''''' $ b ...打印w
...
这个
是
a
样本
行len(l)
5
尊重
Steve
-
Steve Holden http://www.holdenweb.com/
Python网页编程 http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
>
" TB" < TB ****** @ yahoo.com>在消息中写道
news:11 ********************* @ f14g2000cwb.googlegro ups.com ...
是否有一种优雅的方式从未知
大小的列表中分配列表?例如,你怎么能这样做:
a,b,c =(line.split('':''))
如果行可以少于三个字段?
谢谢,
TB
我问了一个非常类似的问题几周前,从各种
的建议,我想出了这个:
line =" AAAA:BBB"
expand = lambda lst,default,minlen:(lst + [default] * minlen)[0:minlen]
a,b,c = expand(line.split(":) ),"",3)
打印一份
打印b
打印c
- 保罗
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
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 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
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
the line.
You can access the elements of the list using a[0], a[2], and so on. For
example:
line = "This:is:a:sample:line"
l = line.split('':'')
l [''This'', ''is'', ''a'', ''sample'', ''line''] for w in l: ... print w
...
This
is
a
sample
line len(l) 5
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
"TB" <tb******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...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 similar question a few weeks ago, and from the various
suggestions, I came up with this:
line = "AAAA:BBB"
expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen]
a,b,c = expand( line.split(":"), "", 3 )
print a
print b
print c
-- Paul
这篇关于列表中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!