Python: remove initial number and underscore from strings in a line (but keep other underscores)

Liquidity

I have a split line (split using .split()) that looks like this:

['a', 'b', 'c', '1_a23_4', '2_b234', '300_235_2_2', '1000_1_1_1_1']

Each string has a variable number of underscores and different combinations of letters/numbers after the first underscore. For any string with a number followed by an underscore, I want to drop the initial number/underscore to get this result:

['a', 'b', 'c', 'a23_4', 'b234', '235_2_2', '1_1_1_1']

This is similar to this question, but I have multiple underscores for some strings in the split line.

Ajax1234

You can use re.sub:

import re
d = ['a', 'b', 'c', '1_a23_4', '2_b234', '300_235_2_2', '1000_1_1_1_1']
new_d = [re.sub('^\d+_', '', i) for i in d]

Output:

['a', 'b', 'c', 'a23_4', 'b234', '235_2_2', '1_1_1_1']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove underscore with number from string in r

Python append line of strings to other line of strings

How to remove specific empty lines from multi line strings in python?

Remove numbers and keep underscore from string in C++

Remove lines by line number in python

How to create list of objects with number of repetitions from other list with underscore?

Remove underscore before extension in strings

How to remove initial and final digits from strings using bash scripting

How to compare strings in python that also contains other ascii symbols like underscores?

How to remove everything after the 2nd underscore but keep the other columns?

Remove last name but keep initial

Pandas - Remove strings from a float number in a column

Python regex wrap digits in underscores and replace all other underscores with dots

how to extract a number from a line in a file where the number is connected to other charchter in python?

Python Remove List of Strings from List of Strings

Python list for different number of strings per line

Remove line number option from grep

Remove a line from file that starts with a number in bash

Remove the new line "\n" from base64 encoded strings in Python3?

Remove a line from a file Python

how to remove spaces and underscores from a string in kdb?

How to remove underscores from json key value

Regex remove underscores from a given string

How to keep indentation from previous line when building strings

How to remove strings in a list and keep only integers in python efficiently

remove Duplicate Object from NSMutableArray with keep sorting and all other fields

print a underscores on the basis of number of chrcters in a string python

Python replace ',' within quotes, but keep other commas, then remove the quotes in a string

remove elements from collection with underscore