How to display all words that contain these characters?

后端 未结 7 1849
后悔当初
后悔当初 2021-01-03 00:05

I have a text file and I want to display all words that contains both z and x characters.

How can I do that ?

相关标签:
7条回答
  • 2021-01-03 00:51

    I do not know the performance of this generator, but for me this is the way:

    from __future__ import print_function
    import string
    
    bookfile = '11.txt' # Alice in Wonderland
    hunted = 'az' # in your case xz but there is none of those in this book
    
    with open(bookfile) as thebook:
        # read text of book and split from white space
        print('\n'.join(set(word.lower().strip(string.punctuation)
                        for word in thebook.read().split()
                        if all(c in word.lower() for c in hunted))))
    """ Output:
    zealand
    crazy
    grazed
    lizard's
    organized
    lazy
    zigzag
    lizard
    lazily
    gazing
    ""
    

    "

    0 讨论(0)
提交回复
热议问题