简体   繁体   中英

How to remove everything from a string using numbers to stop at a certain point

So i am trying to make a function that retrieves a time values in the following formate (example)

2y2m2d 2h2M2s

I want python to remove the letters then split it into a list, or i tell the program to look for char y and remove everything apart from the number in front of it.

How could i achieve this?

(using sqlite3)

Using string splitting we can do this

s="1y2m3d 4h5M6s"
#take the numerical values to the string s_nums. Will look like '1,2,3,4,5,6'
s_nums=','.join([n for n in s if n.isdigit()])
#put each of these int values in an array
values = [int(i) for i in s_nums.split(',')]   
print(values)
>>>[1, 2, 3, 4, 5, 6]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM