简体   繁体   中英

Splitting integers and string from a value

I have a list which include both integers and strings in every value. I want to make a list of values with only integers. ie.;

list = [u'1.0.365-SNAPSHOT', u'1.0.366-SNAPSHOT', u'1.0.367-SNAPSHOT', u'1.0.368-SNAPSHOT', u'1.0.369-SNAPSHOT', u'1.0.370-SNAPSHOT', u'1.0.372-SNAPSHOT', u'1.0.373-SNAPSHOT']

I would like to have list of values which should look like

list = [u'1.0.365', u'1.0.366', u'1.0.367', u'1.0.368', u'1.0.369', u'1.0.370', u'1.0.372', u'1.0.373']

I have been trying to use various functions such as spilt() . But was unable to get the desired result.

This should do the trick:

for file_name in list:
   file_name = file_name.split('-')[0]

Assuming all values in the list end with "-SNAPSHOT" :

lst = [u'1.0.365-SNAPSHOT', u'1.0.366-SNAPSHOT', u'1.0.367-SNAPSHOT', u'1.0.368-SNAPSHOT', u'1.0.369-SNAPSHOT', u'1.0.370-SNAPSHOT', u'1.0.372-SNAPSHOT', u'1.0.373-SNAPSHOT']
lst2 = [x[:-9] for x in lst]

Try to avoid naming lists list . It will override the built-in type.

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