Python 中单行上的多个分割
我想知道是否有一种更紧凑(或Pythonic)的方法来对某些输入字符串进行多次分割。现在我正在做:
[a,bc,de] = 'a,b:c,d/e'.split(',')
[b,c] = bc.split(':')
[d,e] = de.split('/')
I would like to know if there is a more compact (or Pythonic) way of doing several splits of some input string. Now I'm doing:
[a,bc,de] = 'a,b:c,d/e'.split(',')
[b,c] = bc.split(':')
[d,e] = de.split('/')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用正则表达式库。您不需要使用列表来解包,您可以使用元组,如下所示。
I'd use the regular expression library. You don't need to use lists for unpacking, you can use tuples as below.
您可能更适合使用正则表达式拆分方法。不要这样做,它会非常慢,但只是添加答案:(
将左侧展平作为读者的练习(请参阅展平))
You're better of with regex split method probably. Don't do this, it will be crazy slow, but just to add the answer:
(flatten left as an exercise for the reader (see flatten))