-3

basically I need to find if a string (actually a Path) is inside a similar string but more long.

I have this string in a list:

/aa/bb/cc
/aa/bb/cc/11
/aa/bb/cc/22
/aa/bb/dd
/aa/bb/dd/33
/aa/bb/dd/44

I expect to put inside a list only string like:

/aa/bb/cc/11
/aa/bb/cc/22

/aa/bb/dd/33
/aa/bb/dd/44

I need a new list without /aa/bb/cc and /aa/bb/dd because exists /aa/bb/cc/11 and /aa/bb/cc/22, same for /aa/bb/dd, exists /aa/bb/dd/33 and /aa/bb/dd/44 so I do not want the base form /aa/bb/cc and /aa/bb/dd.

I hope I was clear :-D

How can I do thet in Python 3?

Regards

1 Answers1

0

Use regular expressions.

import re

list_1 = ["/aa/bb/cc",
    "/aa/bb/cc/11",
    "/aa/bb/cc/22",
    "/aa/bb/dd",
    "/aa/bb/dd/33",
    "/aa/bb/dd/44"]

regex = re.compile(r'/aa/bb/cc/+.')
obj = filter(regex.search, list_1)
regex2 = re.compile(r'/aa/bb/dd/+.')
obj2 = filter(regex2.search, list_1)
print(list(obj))
print(list(obj2))

Output:

['/aa/bb/cc/11', '/aa/bb/cc/22']

['/aa/bb/dd/33', '/aa/bb/dd/44']