c******n 发帖数: 4965 | 1 perl 里面
( $a , $b ) = split "," , $my_string
如果$my_string 没有含",", 照样可以work.
python 就会出error.
我知道3.x 也可以像perl 一样work,
现在的python 有什么简洁的办法么? | i*****e 发帖数: 1359 | 2 str = 'a b'
a,b = str.split() | M*P 发帖数: 6456 | 3 try except 不成么
【在 c******n 的大作中提到】 : perl 里面 : ( $a , $b ) = split "," , $my_string : 如果$my_string 没有含",", 照样可以work. : python 就会出error. : 我知道3.x 也可以像perl 一样work, : 现在的python 有什么简洁的办法么?
| i*****e 发帖数: 1359 | | l*********s 发帖数: 5409 | 5 why not to use regular expression to specify the delimiter | S*A 发帖数: 7142 | 6 because it does not help.
The problem is not how you split. It is the tuple assign need to have
matching number of element.
If you don't want to use "if" this is what I come up with.
a, b = ("a b".split(',') + [''])[:2]
I think easier to read version:
spt = "a b".split(',')
a = spt.pop(0)
b = spt and spt.pop(0) or ''
【在 l*********s 的大作中提到】 : why not to use regular expression to specify the delimiter
| S*A 发帖数: 7142 | 7 spt = "a b".split(',') + ['']
a, b = spt[:2] | r****t 发帖数: 10904 | 8 python3 如何像 perl 一样 work? 给个例子?
【在 c******n 的大作中提到】 : perl 里面 : ( $a , $b ) = split "," , $my_string : 如果$my_string 没有含",", 照样可以work. : python 就会出error. : 我知道3.x 也可以像perl 一样work, : 现在的python 有什么简洁的办法么?
|
|