Check for regular expression in a list
Tim Chase
python.list at tim.thechases.com
Fri May 26 08:13:53 EDT 2017
On 2017-05-26 13:29, Cecil Westerhof wrote:
> To check if Firefox is running I use:
> if not 'firefox' in [i.name() for i in list(process_iter())]:
>
> It probably could be made more efficient, because it can stop when
> it finds the first instance.
>
> But know I switched to Debian and there firefox is called
> firefox-esr. So I should use:
> re.search('^firefox', 'firefox-esr')
>
> Is there a way to rewrite
> [i.name() for i in list(process_iter())]
>
> so that it returns True when there is a i.name() that matches and
> False otherwise?
> And is it possible to stop processing the list when it found a
> match?
this sounds like an ideal use-case for any():
if any("firefox" in p.name for p in process_iter()):
do_stuff()
or
if any(p.name.startswith("firefox") for p in process_iter()):
do_stuff()
The any() call stops after the True-ish match (and the all() call
stops after the first False-ish match).
-tkc
More information about the Python-list
mailing list