Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get numbers or numbers directly next to letters from a string

I have a list of strings in python:

s = ['64b DRIVE RD', 'DRIVE RD 64', 'Unit 1 Drive RD 64b', '484 South Drive 84b WA', '64Drive']

What I want to do is remove any word that is composed solely of letters. So in the above list, I want my output to be:

['64b', '64', '1 64b', 484 84b, '64Drive']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I able able to get the list with only numbers through the following:

[" ".join(re.findall(r'[0-9]+', i)) for i in s]

output:

['64', '64', '1 64', '484 84', '64']

But am unsure how to keep the letter attached to any number.

>Solution :

Use re.sub a few times with a list comprehension. From right to left, remove all words consisting of letters only, then compress more than one whitespace into a single blank, then remove leading and trailing whitespace.

import re
s = ['64b DRIVE RD', 'DRIVE RD 64', 'Unit 1 Drive RD 64b', '484 South Drive 84b WA', '64Drive']

s = [re.sub(r'(^\s+|\s+$)', '', re.sub(r'\s+', ' ', re.sub(r'\b[A-Za-z]+\b', '', x))) for x in s]
print(s)
# ['64b', '64', '1 64b', '484 84b', '64Drive']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading