【问题标题】:Python Logic in searching String搜索字符串中的 Python 逻辑
【发布时间】:2011-07-05 03:44:51
【问题描述】:
filtered=[]
text="any.pdf"
if "doc" and "pdf" and "xls" and "jpg" not in text:
    filtered.append(text)
print(filtered)

这是我在 Stack Overflow 上的第一篇文章,如果问题中有令人讨厌的地方,请原谅,如果文本不包含以下任何单词,则代码假设附加文本:doc、pdf、xls、jpg。 如果它像这样就可以正常工作:

if "doc" in text:
elif "jpg" in text:
elif "pdf" in text:
elif "xls" in text:
else:
    filtered.append(text)

【问题讨论】:

    标签: python logic


    【解决方案1】:

    如果你打开一个python解释器,你会发现"doc" and "pdf" and "xls" and "jpg"'jpg'是一回事:

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

    因此,您的第一次尝试仅针对“jpg”进行测试,而不是针对所有字符串进行测试。

    有很多方法可以做你想做的事。以下不是最明显的,但很有用:

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

    另一种方法是将for 循环与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)]
    

    这仍然可能导致一些不匹配;上面还过滤了“做某事”、“他是个词匠”等。如果这是个问题,那么您可能需要更复杂的解决方案。

    【讨论】:

    • 我只是简单地补充一点,如果你想忽略大小写,你应该使用str.lower() 方法——即"pdf" in text.lower(),而不是编辑。此外,使用.endswith()(S.Mark 的回答)也很好,因为它不会拒绝像"mypdfprocessor.py" 这样的字符串。
    【解决方案2】:

    如果这些扩展总是在最后,你可以使用.endswith,它可以解析元组。

    if not text.endswith(("doc", "pdf", "xls", "jpg")):
        filtered.append(text)
    

    【讨论】:

    • 只需编辑if not,因为代码会排除以这些字符串结尾的链接,抱歉我不能自己编辑它,因为它告诉我它少于 6 个字符,谢谢
    • +1,endswith 绝对是专门基于扩展进行过滤的方式。
    【解决方案3】:
    basename, ext = os.path.splitext(some_filename)
    if not ext in ('.pdf', '.png'):
       filtered.append(some_filename)
    ....
    

    【讨论】:

      【解决方案4】:

      尝试以下方法:

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

      【讨论】:

        【解决方案5】:

        当前选择的答案很好地解释了语法正确的方法来做你想要做的事情。但是很明显,您正在处理文件扩展名,这些文件扩展名出现在 end [失败:doctor_no.pywhatsupdoc],并且很可能您使用的是 Windows,文件路径中的大小写区别不'不存在[失败: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)
        

        【讨论】:

        • 对不起,我不明白你在说什么,但我使用的是 Ubuntu,主要目标是蜘蛛网站,从源代码中提取源代码后,我排除了包含 javascript 或这些的链接话,还是谢谢
        • 您排除了包含那些字符串的链接,而不是包含那些单词的链接。您将(例如)排除包含单词“doctor”或“dock”或“docket”或“doctored”的链接,并且无法排除包含大写文件名的链接(例如:FUBAR.DOC)。
        • 我使用的是.lower(),所以 FUBAR.DOC 不会被包括在内,但你是对的,所有我不想的单词都会被排除在外。不是所有的词都是扩展的问题,比如start中的javascript,那怎么办??
        • @Mahmoud A. Raouf:“做什么??”:(1)编辑你的问题,说出你真正想要做什么(它主要指向文件扩展名,没有提到“javascript在开始”(你应该解释))。 (2) 取消选择所选答案 (3) 等待解决您问题的答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-23
        • 2020-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多