简体   繁体   中英

Regex/split strings in list for particular element

I have a list that of items in a list that looks like this:

[u'1111 aaaa      20   0  250m 149m 113m S   0.0  2.2 532:09.83 bbbb', u' 5555 cccc      20   0  218m 121m  91m S   0.0  3.3 288:50.20 dddd']

The only thing from each item in the list I am concerned about is 2.2 and 3.3 , but everything in each item is a variable and changes every time the process is run. The format will always be the same however.

Is there a way to regex each item in the list and check this value in each list?

If you want to just get the 2.2 and 3.3 values, you can go without regexps:

data = [u'1111 aaaa      20   0  250m 149m 113m S   0.0  2.2 532:09.83 bbbb', u' 5555 cccc      20   0  218m 121m  91m S   0.0  3.3 288:50.20 dddd']

print([item.split()[9] for item in data]) # yields [u'2.2', u'3.3']

By default split splits by whitespace. And your 2.2 and 3.3 numbers happen to be 10th in each of the blobs. Python uses 0-indexing of lists, so 10th in human terms becomes 9 .

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