F.M
F.M

Reputation: 511

Find a string in a list of list in python

I have a nested list as below:

[['asgy200;f','ssll100','   time is: 10h:00m:12s','xxxxxxx','***','','asgy200;f','frl5100','   time is: 00h:00m:05s','ooo']]

'***' is my delimiter. I want to separate all of seconds in the list in python. First of all with regular expression I want to separate the line that has time is: string but it doesn't work!

I don't know what should I do.

Thanks

Upvotes: 0

Views: 104

Answers (4)

Kasravnd
Kasravnd

Reputation: 107347

You can use a look-ahead regex (r'(?<=time is\:).*') :

>>> [i.group(0).split(':')[2] for i in [re.search(r'(?<=time is\:).*',i) for i in l[0]] if i is not None]
['12s', '05s']

and you can convert them to int :

>>> [int(j.replace('s','')) for j in sec]
[12, 5]

if you want the string of seconds don't convert them to int after replace :

>>> [j.replace('s','') for j in sec]
['12', '05']

Upvotes: 1

gboffi
gboffi

Reputation: 25093

Taking into account your last comment to your Q,

>>> x = [['asgy200;f','ssll100','time is: 10h:00m:12s','xxxxxxx','***','','asgy200;f','frl5100','time is: 00h:00m:05s','ooo']]
>>> print all([w[-3:-1]!='00' for r in x for w in r if w.startswith('time is: ')])
True
>>> 

all and any are two useful builtins...

The thing operates like this, the slower loop is on the sublists (rows) of x, the fastest loop on the items (words)in each row, we pick up only the words that startswith a specific string, and our iterable is made of booleans where we have true if the 3rd last and 2nd last character of the picked word are different from'00'. Finally the all consumes the iterable and returns True if all the second fields are different from '00'.

HTH,

Addendum

Do we want to break out early?

all_secs_differ_from_0 = True

for row in x:
    for word in row:
        if word.startswith('time is: ') and word[-3:-1] == '00':
            all_secs_differ_from_0 = False
            break
    if not all_secs_differ_from_0: break

Upvotes: 0

vks
vks

Reputation: 67988

import re
x=[['asgy200;f','ssll100','time is: 10h:00m:12s','xxxxxxx','***','','asgy200;f','frl5100','time is: 00h:00m:05s','ooo']]
s=str(x)
print re.findall(r"(?<=time is)\s*:\s*[^']*:(\d+)",s)                          

Output:['12', '05']

You can try this.

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174874

You could use capturing groups also. It won't print the seconds if the seconds is exactly equal to 00

>>> lst = [['asgy200;f','ssll100','time is: 10h:00m:12s','xxxxxxx','***','','asgy200;f','frl5100','time is: 00h:00m:05s','ooo']]
>>> [i for i in re.findall(r'time\s+is:\s+\d{2}h:\d{2}m:(\d{2})', ' '.join(lst[0])) if int(i) != 00]
['12', '05']
>>> lst = [['asgy200;f','ssll100','time is: 10h:00m:00s','xxxxxxx','***','','asgy200;f','frl5100','time is: 00h:00m:05s','ooo']]
>>> [i for i in re.findall(r'time\s+is:\s+\d{2}h:\d{2}m:(\d{2})', ' '.join(lst[0])) if int(i) != 00]
['05']

Upvotes: 0

Related Questions