2010-10-18 99 views
6

我有一個文本文件,我想顯示包含z和x字符的所有單詞。如何顯示包含這些字符的所有單詞?

我該怎麼做?

+2

確切的問題在哪裏?你試過什麼了? – 2010-10-18 19:56:32

+0

我不知道如何解析文本文件:) – xRobot 2010-10-18 19:58:30

+0

正則表達式在解析文本時很重要。看看Ishpeck的解決方案。 – Squirrelsama 2010-10-18 20:54:58

回答

11

如果你不想有2個問題:

for word in file('myfile.txt').read().split(): 
    if 'x' in word and 'z' in word: 
     print word 
+1

謝天謝地,你提供了一個答案,*不*使用正則表達式。 – gotgenes 2010-10-18 20:14:13

+0

+1:我非常喜歡這個。我能看到的唯一問題是,你會得到圍繞你的單詞的任何標點符號,而不僅僅是單詞本身。 – 2010-10-18 20:16:34

+0

的確,我正在使用python的「words」定義,這在這裏可能不合理。 – geoffspear 2010-10-18 20:18:52

0

聽起來像是Regular Expressions的工作。閱讀並嘗試一下。如果遇到問題,請更新您的問題,我們可以幫助您解決具體問題。

8

假設你擁有整個文件在內存中一個大字符串,這一個詞的定義是「字母的連續序列」,那麼你可以做這樣的事情:

import re 
for word in re.findall(r"\w+", mystring): 
    if 'x' in word and 'z' in word: 
     print word 
+0

我喜歡這個答案。這是最乾淨的解決方案。如果表現成爲問題,請對照我的解決方案並挑選勝者。 – 2010-10-18 20:14:08

3
>>> import re 
>>> pattern = re.compile('\b(\w*z\w*x\w*|\w*x\w*z\w*)\b') 
>>> document = '''Here is some data that needs 
... to be searched for words that contain both z 
... and x. Blah xz zx blah jal akle asdke asdxskz 
... zlkxlk blah bleh foo bar''' 
>>> print pattern.findall(document) 
['xz', 'zx', 'asdxskz', 'zlkxlk'] 
+0

我可以證實這個作品,比我的回覆好。我將刪除我的這個。 – Ishpeck 2010-10-18 21:03:58

0
>>> import re 
>>> print re.findall('(\w*x\w*z\w*|\w*z\w*x\w*)', 'axbzc azb axb abc axzb') 
['axbzc', 'axzb'] 
1

我不知道該發電機的性能,但對我來說ŧ他是這樣的:

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 
"" 

3

我只是想指出如何笨拙一些正則表達式可以在比較簡單的string methods-based solution provided by Wooble

讓我們來做一些時間安排吧?

#!/usr/bin/env python 
# -*- coding: UTF-8 -*- 

import timeit 
import re 
import sys 

WORD_RE_COMPILED = re.compile(r'\w+') 
Z_RE_COMPILED = re.compile(r'(\b\w*z\w*\b)') 
XZ_RE_COMPILED = re.compile(r'\b(\w*z\w*x\w*|\w*x\w*z\w*)\b') 

########################## 
# Tim Pietzcker's solution 
# https://stackoverflow.com/questions/3962846/how-to-display-all-words-that-contain-these-characters/3962876#3962876 
# 
def xz_re_word_find(text): 
    for word in re.findall(r'\w+', text): 
     if 'x' in word and 'z' in word: 
      print word 


# Tim's solution, compiled 
def xz_re_word_compiled_find(text): 
    pattern = re.compile(r'\w+') 
    for word in pattern.findall(text): 
     if 'x' in word and 'z' in word: 
      print word 


# Tim's solution, with the RE pre-compiled so compilation doesn't get 
# included in the search time 
def xz_re_word_precompiled_find(text): 
    for word in WORD_RE_COMPILED.findall(text): 
     if 'x' in word and 'z' in word: 
      print word 


################################ 
# Steven Rumbalski's solution #1 
# (provided in the comment) 
# https://stackoverflow.com/questions/3962846/how-to-display-all-words-that-contain-these-characters/3963285#3963285 
def xz_re_z_find(text): 
    for word in re.findall(r'(\b\w*z\w*\b)', text): 
     if 'x' in word: 
      print word 


# Steven's solution #1 compiled 
def xz_re_z_compiled_find(text): 
    pattern = re.compile(r'(\b\w*z\w*\b)') 
    for word in pattern.findall(text): 
     if 'x' in word: 
      print word 


# Steven's solution #1 with the RE pre-compiled 
def xz_re_z_precompiled_find(text): 
    for word in Z_RE_COMPILED.findall(text): 
     if 'x' in word: 
      print word 


################################ 
# Steven Rumbalski's solution #2 
# https://stackoverflow.com/questions/3962846/how-to-display-all-words-that-contain-these-characters/3962934#3962934 
def xz_re_xz_find(text): 
    for word in re.findall(r'\b(\w*z\w*x\w*|\w*x\w*z\w*)\b', text): 
     print word 


# Steven's solution #2 compiled 
def xz_re_xz_compiled_find(text): 
    pattern = re.compile(r'\b(\w*z\w*x\w*|\w*x\w*z\w*)\b') 
    for word in pattern.findall(text): 
     print word 


# Steven's solution #2 pre-compiled 
def xz_re_xz_precompiled_find(text): 
    for word in XZ_RE_COMPILED.findall(text): 
     print word 


################################# 
# Wooble's simple string solution 
def xz_str_find(text): 
    for word in text.split(): 
     if 'x' in word and 'z' in word: 
      print word 


functions = [ 
     'xz_re_word_find', 
     'xz_re_word_compiled_find', 
     'xz_re_word_precompiled_find', 
     'xz_re_z_find', 
     'xz_re_z_compiled_find', 
     'xz_re_z_precompiled_find', 
     'xz_re_xz_find', 
     'xz_re_xz_compiled_find', 
     'xz_re_xz_precompiled_find', 
     'xz_str_find' 
] 

import_stuff = functions + [ 
     'text', 
     'WORD_RE_COMPILED', 
     'Z_RE_COMPILED', 
     'XZ_RE_COMPILED' 
] 


if __name__ == '__main__': 

    text = open(sys.argv[1]).read() 
    timings = {} 
    setup = 'from __main__ import ' + ','.join(import_stuff) 
    for func in functions: 
     statement = func + '(text)' 
     timer = timeit.Timer(statement, setup) 
     min_time = min(timer.repeat(3, 10)) 
     timings[func] = min_time 


    for func in functions: 
     print func + ":", timings[func], "seconds" 

運行在plaintext copy of Moby Dick這個腳本Project Gutenberg獲得的,在Python 2.6中,我得到以下計時:

xz_re_word_find: 1.21829485893 seconds 
xz_re_word_compiled_find: 1.42398715019 seconds 
xz_re_word_precompiled_find: 1.40110301971 seconds 
xz_re_z_find: 0.680151939392 seconds 
xz_re_z_compiled_find: 0.673038005829 seconds 
xz_re_z_precompiled_find: 0.673489093781 seconds 
xz_re_xz_find: 1.11700701714 seconds 
xz_re_xz_compiled_find: 1.12773990631 seconds 
xz_re_xz_precompiled_find: 1.13285303116 seconds 
xz_str_find: 0.590088844299 seconds 

在Python 3.1(使用2to3修復打印報表後),我得到以下時序:

xz_re_word_find: 2.36110496521 seconds 
xz_re_word_compiled_find: 2.34727501869 seconds 
xz_re_word_precompiled_find: 2.32607793808 seconds 
xz_re_z_find: 1.32204890251 seconds 
xz_re_z_compiled_find: 1.34104800224 seconds 
xz_re_z_precompiled_find: 1.34424304962 seconds 
xz_re_xz_find: 2.33851099014 seconds 
xz_re_xz_compiled_find: 2.29653286934 seconds 
xz_re_xz_precompiled_find: 2.32416701317 seconds 
xz_str_find: 0.656699895859 seconds 

我們可以看到,基於正則表達式的功能,往往需要兩倍的時間來的〜應變運行g是基於方法的函數,在Python 3中是超過3倍。對於一次性解析(沒有人會錯過這些毫秒),時間差異是微不足道的,但對於必須多次調用該函數的情況,基於字符串方法的方法既簡單又快捷。

+0

我也喜歡字符串方法。但是,這是一個挑剔。我更改了zx_re_find(text)的定義,它比純字符串方法快4倍: def zx_re_find(text): pat = re.compile('(\ b \ w * z \ w * \ b)') word在pat.findall(文本): 如果文字中有'x': 打印文字 – 2010-10-18 21:25:43

+0

@Steven我已經更新了我的答案,包括在評論中包含您的建議解決方案以及您提供的答案解答與字符串方法相比,任何正則表達式都不會獲得4倍的性能。對我來說,可再生能源解決方案仍然落後。你用什麼文字來測試你的表現? – gotgenes 2010-10-18 22:33:00

+0

@gotgenes我使用了與Moby Dick相同的明文副本。我在Windows XP上使用了python 2.7(嗯,在我工作的筆記本電腦上忘了芯片)。我記得字符串0.311的前三位數字和正則表達式的0.088(不是真正的4倍,但接近)。我堅持認爲,如果要求更加複雜,正則表達式將獲得簡單性和性能。 – 2010-10-18 23:47:29

相關問題