【问题标题】:how to print words excluding particular letter string?如何打印不包括特定字母字符串的单词?
【发布时间】:2015-07-30 06:57:21
【问题描述】:

我需要编写一个程序,提示用户输入一串禁用字母,然后打印不包含任何字母的单词数

这就是我所拥有的:

fin=open("demo.txt",'r')
letters=str(raw_input("Enter the key you want to exclude"))
def avoid ():   
    for line in fin:
        word = line.strip()
        for l in letters:
            if l not in word: 
                print word          
avoid()

如果我使用 in 关键字,它会打印所有具有特定字符串的单词。但是当我使用 not in 时,它的工作方式就不一样了。

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    这样的事情应该可以工作:

    for l in letters:
        if l in word:
            break  # we found the letter in the word, so stop looping
    else:
        # This will execute only when the loop above didn't find
        # any of the letters. 
        print(word)
    

    【讨论】:

    • 感谢您的解决方案,但程序仍然避免仅使用字符串中的第一个字母
    • 你确定吗?请注意,上面的代码应该在for word in ... 循环内,因为它只显示“仅打印不包含任何字母的单词”逻辑。
    【解决方案2】:

    最简单的方法是把字母组成一个集合,把每一行分成单词,然后总结有多少次没有禁止字母的单词:

    with open("demo.txt", 'r') as f:
        letters = set(raw_input("Enter the key you want to exclude"))
        print(sum(not letters.intersection(w) for line in f for w in line.split()))
    

    你也可以使用str.translate,检查翻译后字长是否变化:

    with open("demo.txt", 'r') as f:
        letters = raw_input("Enter the key you want to exclude")
        print(sum(len(w.translate(None, letters)) == len(w) for line in f for w in line.split()))
    

    如果尝试删除任何letters 后单词的长度相同,则该单词不包含任何字母。

    或者使用any:

    with open("in.txt", 'r') as f:
        letters = raw_input("Enter the key you want to exclude")
        print(sum(not any(let in w for let in letters) for line in f for w in line.split()))
    

    any(let in w for let in letters) 会检查字母中的每个字母,看看每个单词中是否有任何字母,如果找到禁止的字母,它将短路并返回True,否则如果单词中没有出现字母,则返回False然后继续下一个词。

    你不能使用if l in word,除非你每行只有一个单词,你需要拆分成单独的单词。

    使用您自己的代码,当您在单词中找到一个字母时,您只需要中断,否则如果我们遍历所有字母但未找到匹配项,则打印该单词:

    for line in fin:
        word = line.strip()
        for l in letters:
            if l  in word:
                break 
        else:
             print word
    

    在循环中做你想做的事情的pythonic方法是使用any:

    for line in fin:
        word = line.strip()
        if not any(l not in word for l in letters): 
             print word
    

    这相当于 break/else 更好。

    如果您想要总和,那么您需要随时跟踪:

      total = 0
      for line in fin:
          word = line.strip()
          if not any(l not in word  for l in letters): 
               total += 1
     print(total)
    

    这是一种效率较低的方法:

    print(sum(not any(let in w.rstrip() for let in letters) for word in f))
    

    【讨论】:

    • 感谢您的解决方案伙伴
    • @def_0101,不用担心,使用any 是可行的方法。
    【解决方案3】:

    只是另一种方式。

    exclude = set(raw_input("Enter the key you want to exclude"))
    with open("demo.txt") as f:
        print len(filter(exclude.isdisjoint, f.read().split()))
    

    【讨论】:

    • 感谢您的解决方案伙伴
    • 不客气。不过,你还在等什么吗? (我注意到您尚未接受我们的任何答案)。
    【解决方案4】:

    这样更好:

        forbidden='ab'
        listofstring=['a','ab','acc','aaabb']
        for i in listofstring:
            if not any((c in i) for c in forbidden):
                print i
    

    【讨论】:

    • 这是错误的,您正在检查单词中是否所有禁用字母都不是任何
    • @PadraicCunningham """不包含其中任何一个的单词""" 我由此解释。我认为“所有”是要走的路。任何建议
    • 感谢您的解决方案
    • 如果 a 或 b 出现在字符串中,那么这将打印字符串。您正在检查单词中是否所有禁止的字母都不是任何
    • @PadraicCunningham 现在好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2019-05-03
    • 2017-04-13
    • 2016-04-07
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多