回答
如果你不想有2個問題:
for word in file('myfile.txt').read().split():
if 'x' in word and 'z' in word:
print word
謝天謝地,你提供了一個答案,*不*使用正則表達式。 – gotgenes 2010-10-18 20:14:13
+1:我非常喜歡這個。我能看到的唯一問題是,你會得到圍繞你的單詞的任何標點符號,而不僅僅是單詞本身。 – 2010-10-18 20:16:34
的確,我正在使用python的「words」定義,這在這裏可能不合理。 – geoffspear 2010-10-18 20:18:52
聽起來像是Regular Expressions的工作。閱讀並嘗試一下。如果遇到問題,請更新您的問題,我們可以幫助您解決具體問題。
假設你擁有整個文件在內存中一個大字符串,這一個詞的定義是「字母的連續序列」,那麼你可以做這樣的事情:
import re
for word in re.findall(r"\w+", mystring):
if 'x' in word and 'z' in word:
print word
我喜歡這個答案。這是最乾淨的解決方案。如果表現成爲問題,請對照我的解決方案並挑選勝者。 – 2010-10-18 20:14:08
>>> 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']
我可以證實這個作品,比我的回覆好。我將刪除我的這個。 – Ishpeck 2010-10-18 21:03:58
>>> import re
>>> print re.findall('(\w*x\w*z\w*|\w*z\w*x\w*)', 'axbzc azb axb abc axzb')
['axbzc', 'axzb']
我不知道該發電機的性能,但對我來說ŧ他是這樣的:
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
""
「
我只是想指出如何笨拙一些正則表達式可以在比較簡單的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倍。對於一次性解析(沒有人會錯過這些毫秒),時間差異是微不足道的,但對於必須多次調用該函數的情況,基於字符串方法的方法既簡單又快捷。
我也喜歡字符串方法。但是,這是一個挑剔。我更改了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
@Steven我已經更新了我的答案,包括在評論中包含您的建議解決方案以及您提供的答案解答與字符串方法相比,任何正則表達式都不會獲得4倍的性能。對我來說,可再生能源解決方案仍然落後。你用什麼文字來測試你的表現? – gotgenes 2010-10-18 22:33:00
@gotgenes我使用了與Moby Dick相同的明文副本。我在Windows XP上使用了python 2.7(嗯,在我工作的筆記本電腦上忘了芯片)。我記得字符串0.311的前三位數字和正則表達式的0.088(不是真正的4倍,但接近)。我堅持認爲,如果要求更加複雜,正則表達式將獲得簡單性和性能。 – 2010-10-18 23:47:29
- 1. 如何找到一些包含在字符串中的單詞?
- 2. 如果文本字符串包含某些單詞,請將這些單詞包裹在span標記中
- 3. 如何刪除包含特定單詞的所有行,但包含某些單詞的行除外?
- 4. 如何匹配包含非單詞字符的單詞?
- 5. 檢查列表包含在字符串中的所有單詞
- 6. 如何匹配包含給定數字的所有單詞?
- 7. 突出顯示字符串中的單詞,如果它包含關鍵字
- 8. 查找包含當前字符的所有字詞
- 9. 包含unicode字符的分詞包含的字詞
- 10. 正則表達式:匹配所有包含單詞的單詞
- 11. 試圖顯示包含字符串的行時顯示的所有行
- 12. 包含數字的單詞
- 13. jquery hide /顯示包含帶複選框的單詞的所有tr
- 14. 如何搜索第二個字符串中包含的所有單詞的一個字符串?
- 15. 如何將單詞數組轉換爲包含單詞字符的數組?
- 16. 如何匹配包含連字符的單詞?
- 17. 如何使用componentsSeparatedByString忽略包含子字符串的單詞:?
- 18. 如何索引Solr中包含特殊字符的單詞
- 19. 如何匹配不包含單詞的字符串?
- 20. 如何搜索ArrayList中包含多個單詞的字符串?
- 21. 如何突出顯示替換所選單詞的單詞?
- 22. PHP,從包含特定單詞的大字符串中刪除所有行
- 23. 如何匹配連續不包含兩個數字的所有單詞?
- 24. 包含某些類字符串的jQuery顯示元素
- 25. 比較兩個數組:一個包含單詞,另一個包含短語。有些是由這些詞組成的,有些並非如此
- 26. Haskell計算包含字符串中特定字符的單詞
- 27. COUNTIF單元格包含這些字符串之一
- 28. 使用JQuery突出顯示包含指定單詞和之後的所有單詞?
- 29. 查找id不包含某些字符的所有文本框
- 30. 如何檢查鏈接字符串是否包含單詞? (PHP)
確切的問題在哪裏?你試過什麼了? – 2010-10-18 19:56:32
我不知道如何解析文本文件:) – xRobot 2010-10-18 19:58:30
正則表達式在解析文本時很重要。看看Ishpeck的解決方案。 – Squirrelsama 2010-10-18 20:54:58