मेरे पास एक टेक्स्ट फ़ाइल है और मैं उन सभी शब्दों को प्रदर्शित करना चाहता हूं जिनमें ज़ेड और एक्स दोनों वर्ण शामिल हैं।इन वर्णों वाले सभी शब्दों को कैसे प्रदर्शित करें?
मैं यह कैसे कर सकता हूं?
मेरे पास एक टेक्स्ट फ़ाइल है और मैं उन सभी शब्दों को प्रदर्शित करना चाहता हूं जिनमें ज़ेड और एक्स दोनों वर्ण शामिल हैं।इन वर्णों वाले सभी शब्दों को कैसे प्रदर्शित करें?
मैं यह कैसे कर सकता हूं?
आप 2 समस्याओं के लिए नहीं करना चाहते हैं:
for word in file('myfile.txt').read().split():
if 'x' in word and 'z' in word:
print word
भलाई का शुक्रिया आपने एक उत्तर दिया है कि * नियमित अभिव्यक्तियों का उपयोग नहीं करता है। – gotgenes
+1: मुझे यह बहुत पसंद है। एकमात्र समस्या जो मैं देख सकता हूं वह यह है कि आपको अपने शब्दों के आस-पास कोई भी विराम चिह्न मिलेगा, केवल शब्दों ही नहीं। –
सच है, मैं "शब्द" की पायथन की परिभाषा का उपयोग कर रहा हूं, जो यहां अनुचित हो सकता है। – geoffspear
Regular Expressions के लिए नौकरी की तरह लगता है। इसे पढ़ें और इसे आज़माएं। यदि आप समस्याओं में भाग लेते हैं, तो अपना प्रश्न अपडेट करें और हम आपको विशिष्टताओं के साथ मदद कर सकते हैं।
मान लिया जाये कि आप स्मृति में एक बड़ी स्ट्रिंग के रूप में पूरी फ़ाइल है, और है कि एक शब्द की परिभाषा है "पत्र का एक सन्निहित अनुक्रम", तो आप कुछ इस तरह कर सकता है:
import re
for word in re.findall(r"\w+", mystring):
if 'x' in word and 'z' in word:
print word
मुझे यह जवाब पसंद है। यह सबसे साफ समाधान है। यदि प्रदर्शन एक मुद्दा बन जाता है, तो मेरे समाधान के खिलाफ इसे समय दें और विजेता चुनें। –
>>> 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
>>> 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"
, Project Gutenberg से प्राप्त अजगर 2.6 पर एक plaintext copy of Moby Dick पर इस स्क्रिप्ट चल रहा है, मैं निम्नलिखित समय मिलता है: (2to3 का उपयोग कर प्रिंट बयान को ठीक करने के बाद)
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
अजगर 3.1 में, मैं मिल निम्नलिखित समय:
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
हम देख सकते हैं कि नियमित अभिव्यक्ति आधारित कार्यों strin के रूप में चलाने के लिए दोगुना समय लग जाते हैं पाइथन 2.6 में जी विधियों-आधारित फ़ंक्शन, और पाइथन 3 में 3 गुना अधिक समय तक। अंतर अंतर एक-बंद पार्सिंग के लिए छोटा है (कोई भी उन मिलीसेकंड को याद करने वाला नहीं है), लेकिन उन मामलों के लिए जहां फ़ंक्शन को कई बार बुलाया जाना चाहिए, स्ट्रिंग विधियों-आधारित दृष्टिकोण दोनों सरल और तेज़ हैं।
मैं भी स्ट्रिंग विधियों को प्राथमिकता देता हूं। लेकिन, यहाँ एक नाइटपिक है।मैंने zx_re_find (टेक्स्ट) की परिभाषा को बदल दिया है और यह शुद्ध स्ट्रिंग विधि से 4x तेज है: def zx_re_find (टेक्स्ट): pat = re.compile ('(\ b \ w * z \ w * \ b)') के लिए pat.findall (text) में शब्द: यदि शब्द में 'x' है: प्रिंट शब्द –
@ स्टीवन मैंने अपना जवाब अपडेट किया है जिसमें टिप्पणी में आपके सुझाए गए समाधान और समाधान के रूप में आपके द्वारा प्रदान किए गए समाधान शामिल हैं, और किया स्ट्रिंग विधि की तुलना में किसी भी नियमित अभिव्यक्ति द्वारा 4 एक्स प्रदर्शन प्राप्त नहीं करें। मेरे लिए, आरई समाधान अभी भी पीछे पीछे है। आपके प्रदर्शन का परीक्षण करने के लिए आपने किस पाठ का उपयोग किया था? – gotgenes
@gotgenes मैंने मोबी डिक की एक ही सादा पाठ प्रतिलिपि का उपयोग किया। मैंने विंडोज एक्सपी पर पायथन 2.7 का इस्तेमाल किया (हम्म .. मेरे काम लैपटॉप में चिप भूल गया)। मुझे स्ट्रिंग के लिए स्ट्रिंग और 0.088 के लिए समय 0.311 के पहले 3 अंक याद हैं (वास्तव में 4x नहीं, लेकिन बंद)। मैं यह मानता हूं कि यदि आवश्यकताएं और अधिक जटिल थीं, तो रेगेक्स सादगी और प्रदर्शन में लाभान्वित होगा। –
सही समस्या कहां है? अब तक तुमने क्या प्रयास किये हैं? –
मुझे नहीं पता कि पाठ फ़ाइल को कैसे पार्स करना है :) – xRobot
जब टेक्स्ट पार्सिंग की बात आती है तो नियमित अभिव्यक्तियां राजा होती हैं। इशपेक के समाधान को देखो। – Squirrelsama