CEamonn
CEamonn

Reputation: 925

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?

Upvotes: 0

Views: 31

Answers (1)

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24251

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.

Upvotes: 1

Related Questions