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.
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.
Comments