Reputation: 625
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.
Upvotes: 2
Views: 550
Reputation:
>>> l=['a', 'b', 'c', '1_a23_4', '2_b234', '300_235_2_2', '1000_1_1_1_1']
>>> l=["_".join(i.split("_")[1:]) if "_" in i else i for i in l]
>>> l
['a', 'b', 'c', 'a23_4', 'b234', '235_2_2', '1_1_1_1']
Upvotes: 1
Reputation: 71451
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']
Upvotes: 3