I have a list of strings
l = [
'/api/users/*',
'/api/account/*
]
And paths are like
/api/users/add/
/api/users/edit/1
/api/users/
/api/account/view/1
/api/account/
How can I perform filter for the paths if they exist in the list l
.
condition like
'/api/users/add/' in l
should return True
and for all given paths above.
If I understand correctly, you want to see if the wildcard pattern would hold true. For this you can use the fnmatch
module from glob
. Supposing you have this:
l = [
'/api/users/*',
'/api/account/*'
]
paths = [
'/api/users/add/'
'/api/users/edit/1',
'/api/users/',
'/api/account/view/1',
'/api/account/',
'/non/existent/path'
]
You could get this:
>>> import fnmatch
>>> [any(fnmatch.fnmatch(path, pat) for pat in l) for path in paths]
[True, True, True, True, False]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With