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']
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']