0

I'm having this simple code:

print( re.split(' |,', '156 for 23,5 or 15') )
# ['156', 'for', '23', '5', 'or', '15']

The question is how to return digits as integers and text as strings (it is already)... So getting this instead:

# [156, 'for', 23, 5, 'or', 15]
Enissay
  • 4,969
  • 3
  • 29
  • 56

1 Answers1

2

Using Regex, you can't.

However, you can use a list comprehension, a conditional expression, and str.isdigit:

>>> import re
>>> [int(x) if x.isdigit() else x for x in re.split(' |,', '156 for 23,5 or 15')]
[156, 'for', 23, 5, 'or', 15]
>>>
Community
  • 1
  • 1