2

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('/')
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • this would be cool: `[a,[b,c],[d,e]]=[a,@.split(":"),@.split("/")] = 'a,b:c,d/e'.split(',')`. don't think there's another way though – Claudiu Jun 30 '11 at 20:52

2 Answers2

8

I'd use the regular expression library. You don't need to use lists for unpacking, you can use tuples as below.

import re
regex = re.compile(r'[,:/]')
a, b, c, d, e = regex.split('a,b:c,d/e')
Ryan
  • 15,016
  • 6
  • 48
  • 50
1

You're better of with regex split method probably. Don't do this, it will be crazy slow, but just to add the answer:

a,b,c,d,e = flatten( (x.split(',') for x in y.split(':')) for y in z.split('/')  )

(flatten left as an exercise for the reader (see flatten))

Community
  • 1
  • 1
viraptor
  • 33,322
  • 10
  • 107
  • 191