I have a list in Python that looks like this:
['29382 this is something', '2938535 hello there', '392835 dont care for this', '22024811 yup']
I need to process it so that it like this:
['29382', '2938535', '392835', '22024811']
How would I go on about doing this? I guess I could use re, but I don't know how to apply it in this situation.
You do not need regex
for this, you can use split
within a list comprehension.
>>> l = ['29382 this is something', '2938535 hello there', '392835 dont care for this', '22024811 yup']
>>> [i.split(' ', 1)[0] for i in l]
['29382', '2938535', '392835', '22024811']
Something like
>>> l=['29382 this is something', '2938535 hello there', '392835 dont care for this', '22024811 yup']
>>> import re
>>> [ re.sub(r'\D', '', x) for x in l]
['29382', '2938535', '392835', '22024811']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.