Python logic in string search

filtered=[]
text="any.pdf"
if "doc" and "pdf" and "xls" and "jpg" not in text:
    filtered.append(text)
print(filtered)

This is my first post on stack overflow, so sorry if there is anything annoying in the question. The code suggests adding text if the text does not contain any of these words: doc, pdf, xls, jpg. It works fine if it looks like:

if "doc" in text:
elif "jpg" in text:
elif "pdf" in text:
elif "xls" in text:
else:
    filtered.append(text)
+3
source share
5 answers

If you open the python interpreter, you will find that "doc" and "pdf" and "xls" and "jpg"it is the same as 'jpg':

>>> "doc" and "pdf" and "xls" and "jpg"
'jpg'

So, instead of testing all the lines, your first attempt only checks the "jpg".

There are several ways to do what you want. The following is not the most obvious, but useful:

if not any(test_string in text for test_string in ["doc", "pdf", "xls", "jpg"]):
    filtered.append(text)

Another approach would be to use a loop forin combination with an operator else:

for test_string in ["doc", "pdf", "xls", "jpg"]:
    if test_string in text:
        break
else: 
    filtered.append(text)

, :

tofilter = ["one.pdf", "two.txt", "three.jpg", "four.png"]
test_strings = ["doc", "pdf", "xls", "jpg"]
filtered = [s for s in tofilter if not any(t in s for t in test_strings)]

, :

text_list = generate_text_list() # or whatever you do to get a text sequence
extensions = ['.doc', '.pdf', '.xls', '.jpg']
words = ['some', 'words', 'to', 'filter']
text_list = [text for text in text_list if not text.endswith(tuple(extensions))]
text_list = [text for text in text_list if not any(word in text for word in words)]

; " -", " - " .. , .

+6

, .endswith .

if not text.endswith(("doc", "pdf", "xls", "jpg")):
    filtered.append(text)
+4
basename, ext = os.path.splitext(some_filename)
if not ext in ('.pdf', '.png'):
   filtered.append(some_filename)
....
+3

:

if all(substring not in text for substring in ['doc', 'pdf', 'xls', 'jpg']):
     filtered.append(text)
+1

, , . , , [fail: doctor_no.py, whatsupdoc], , Windows, [fail: FUBAR.DOC].

:

# setup
import os.path
interesting_extensions = set("." + x for x in "doc pdf xls jpg".split())

# each time around
basename, ext = os.path.splitext(text)
if ext.lower() not in interesting_extensions:
    filtered.append(text)
+1

Source: https://habr.com/ru/post/1795018/


All Articles