以下是我的工作示例:
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)
我想要与test相反的输出url。例如"steve"需要Url作为输出!所需输出:输出中需要的所有steve url。
希望我澄清了自己,我得到了一些回应,谢谢。
我不知道你为什么要压缩名字。您可以使用not
any()
来测试是否不匹配:
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)]
这将给你:
'[register/steve/users_photo1.jpg',
'register/steve/users_photo2.jpg',
'register/steve/users_photo1.jpg',
'register/steve/users_photo2.jpg']
如果test
很长,这些可能是一些优化,但这应该适用于正常大小的输入。
我将使用列表推导和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)
这个打印:
['register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg',
'register/steve/users_photo1.jpg', 'register/steve/users_photo2.jpg']