Home > other >  extract path name based on a string
extract path name based on a string

Time:05-23

Below is my worked examples:

from itertools import zip_longest

test2 = ['register/adam/users_photo3.jpg', 'register/adam/users_photo4.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/adam/users_photo3.jpg', 'register/adam/users_photo4.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg']

test = ["justin","adam"]


filter_list = []
for p,q in zip_longest(test,list_of_files):
    for r in list_of_files:
        if str(p) in r:
            filter_list.append(r)       


testmain=[p for p,r in zip_longest(test2,filter_list) if str(r) not in str(p)]
print(testmain) 

I want the opposite output url of test. example "steve" url needed as output ! Needed output: All steve urls needed in output.

Hope I clarified myself and I get some response, Thank you.

CodePudding user response:

I'm not sure why you are zipping the names. You can use not any() to test for no matches:

test2 = ['register/adam/users_photo3.jpg', 'register/adam/users_photo4.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/adam/users_photo3.jpg', 'register/adam/users_photo4.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg', 
'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg']

test = ["justin","adam"]

[url for url in test2 if not any(name in url for name in test)]

This will give you:

'[register/steve/users_photo1.jpg',
 'register/steve/users_photo2.jpg',
 'register/steve/users_photo1.jpg',
 'register/steve/users_photo2.jpg']

If test is long, these are probably some optimizations, but this should work for normal sized inputs.

CodePudding user response:

I would use a list comprehension along with re.search:

test2 = ['register/adam/users_photo3.jpg', 
    'register/adam/users_photo4.jpg', 
    'register/justin/users_photo1.jpg', 
    'register/justin/users_photo2.jpg', 
    'register/adam/users_photo3.jpg', 
    'register/adam/users_photo4.jpg', 
    'register/justin/users_photo1.jpg', 
    'register/justin/users_photo2.jpg', 
    'register/steve/users_photo1.jpg', 
    'register/steve/users_photo2.jpg', 
    'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg', 
    'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg', 
    'register/justin/users_photo1.jpg', 'register/justin/users_photo2.jpg']

test = ["justin", "adam"]
regex = r'/(?:'   '|'.join(test)   r')/'
output = [x for x in test2 if not re.search(regex, x)]
print(output)

This prints:

['register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg',
 'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg']
  • Related