If I've a string like, 'level1-4car10'
, I want to extract all those numbers (integers, specifically) as numbers, in a list.
When I run,
re.split(r'(\d+)', 'level1-4car10')
That does half the job, giving me a result of,
['level', '1', '-', '4', 'car', '10', '']
But what I'm looking for is,
['level', 1, '-', 4, 'car', 10, '']
The 1, 4 and 10 are real integers now, not numbers. Currently, using re.split
with capturing, I'll have to run through the returned list to do that, and I'm just wondering if there's any other option available to me to do this in a more one-pass way, like the key
section of sorted
.
edit: I've reworded the question, specifically stating single pass
.
This is also different to the answer regarding Python: Extract numbers from a string as I don't just want the numbers, I want the numbers and the not number
in a list ordered by how they appear in the sentence.