Remove numbers from a list but keep all characters

Hi all!

I was scrolling down through the forum but I could’t find the right solution.

I have a list and I’m looking for a script that helps to remove numbers 0…9, but keep everything else.
For ex.:

List:
NE-NEX-Z-W01
NE-NEX-Z-W02
.
.
NE-NEX-Z-W101

The output suppose to be:

NE-NEX-Z-W
NE-NEX-Z-W
.
.
NE-NEX-Z-W

I found some good scripts how to extract only numbers but I can’t figure this out .

This one works fine for extracting numbers:

``````output = []
for x in IN[0]:
output.append(float(filter(str.isdigit, x)))
OUT = output
``````

*Note that removing characters from left to right is not an option because the amount of characters may vary.

``````output = []
for x in IN[0]:
output.append(filter(lambda v: not str.isdigit(v), x))
OUT = output``````
2 Likes

Utilise the following should help sort your issue, but do watch out that it will remove all numbers no matter where they are in your string.

1 Like

That’s exactly what I was looking for! Thanks.

I really appreciate for a quick answer. Numbers will be 100% at the end of the string so I don’t need to worry about. Thanks!

If you could end up with numbers any where else that you will need to keep within then use the following code. If the numbers will always be at the end then either mine or @Thomas_Corrie solution will work perfectly

Code:

``````output=[ ]
for a in IN[0]:
newlist = a.split("-")
stringpos = 0
for sp in newlist[:-1]:
stringpos = stringpos + len(sp)
stringpos = stringpos + len(newlist[:-1])
templist=[ ]
for i, c in enumerate(newlist[-1]):
if c.isdigit():
templist.append(i)
output.append(a[0:(stringpos+min(templist))])
OUT=output
``````

Edited: updated image and code to something more robust.

2 Likes