How to display all words that contain these characters?

后端 未结 7 1851
后悔当初
后悔当初 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
    ""
    

    "

提交回复
热议问题