python - क्या पाइथन में स्ट्रिंग 'युक्त' सबस्ट्रिंग विधि है?
string substring (10)
मैं Python में string.contains
या string.indexof
विधि की तलाश में string.indexof
।
मैं करना चाहता हूँ:
if not somestring.contains("blah"):
continue
क्या पाइथन में स्ट्रिंग में स्ट्रिंग विधि होती है?
हां, लेकिन पायथन में एक तुलना ऑपरेटर है जिसका आपको उपयोग करना चाहिए, क्योंकि भाषा इसका उपयोग करने का इरादा रखती है, और अन्य प्रोग्रामर आपको इसका उपयोग करने की उम्मीद करेंगे। वह कीवर्ड है, जिसका उपयोग तुलना ऑपरेटर के रूप में किया जाता है:
>>> 'foo' in '**foo**'
True
विपरीत (पूरक), जो मूल प्रश्न पूछता है, इसमें not in
:
>>> 'foo' not in '**foo**' # returns False
False
यह अर्थात् not 'foo' in '**foo**'
जैसा not 'foo' in '**foo**'
लेकिन यह पठनीयता सुधार के रूप में भाषा में अधिक पढ़ने योग्य और स्पष्ट रूप से प्रदान किया गया है।
__contains__
, find
, और index
का उपयोग करने से बचें
जैसा कि वादा किया गया है, यहां विधि contains
है:
str.__contains__('**foo**', 'foo')
True
आता है। आप सुपरस्टिंग के उदाहरण से इस फ़ंक्शन को भी कॉल कर सकते हैं:
'**foo**'.__contains__('foo')
लेकिन मत करो। अंडरस्कोर से शुरू होने वाले तरीके को अर्थात् निजी माना जाता है। इसका उपयोग करने का एकमात्र कारण यह है कि जब कार्यक्षमता in
और विस्तार not in
है (उदाहरण के लिए यदि subclassing str
):
class NoisyString(str):
def __contains__(self, other):
print('testing if "{0}" in "{1}"'.format(other, self))
return super(NoisyString, self).__contains__(other)
ns = NoisyString('a string with a substring inside')
और अब:
>>> 'substring' in ns
testing if "substring" in "a string with a substring inside"
True
साथ ही, निम्न स्ट्रिंग विधियों से बचें:
>>> '**foo**'.index('foo')
2
>>> '**foo**'.find('foo')
2
>>> '**oo**'.find('foo')
-1
>>> '**oo**'.index('foo')
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
'**oo**'.index('foo')
ValueError: substring not found
अन्य भाषाओं में सबस्ट्रिंग्स के लिए सीधे परीक्षण करने के लिए कोई तरीका नहीं हो सकता है, और इसलिए आपको इन प्रकार के तरीकों का उपयोग करना होगा, लेकिन पायथन के साथ, तुलनात्मक ऑपरेटर का उपयोग करना अधिक कुशल है।
प्रदर्शन तुलना
हम एक ही लक्ष्य को पूरा करने के विभिन्न तरीकों की तुलना कर सकते हैं।
import timeit
def in_(s, other):
return other in s
def contains(s, other):
return s.__contains__(other)
def find(s, other):
return s.find(other) != -1
def index(s, other):
try:
s.index(other)
except ValueError:
return False
else:
return True
perf_dict = {
'in:True': min(timeit.repeat(lambda: in_('superstring', 'str'))),
'in:False': min(timeit.repeat(lambda: in_('superstring', 'not'))),
'__contains__:True': min(timeit.repeat(lambda: contains('superstring', 'str'))),
'__contains__:False': min(timeit.repeat(lambda: contains('superstring', 'not'))),
'find:True': min(timeit.repeat(lambda: find('superstring', 'str'))),
'find:False': min(timeit.repeat(lambda: find('superstring', 'not'))),
'index:True': min(timeit.repeat(lambda: index('superstring', 'str'))),
'index:False': min(timeit.repeat(lambda: index('superstring', 'not'))),
}
और अब हम देखते हैं कि इनका उपयोग दूसरों की तुलना में बहुत तेज़ है। बराबर ऑपरेशन करने के लिए कम समय बेहतर है:
>>> perf_dict
{'in:True': 0.16450627865128808,
'in:False': 0.1609668098178645,
'__contains__:True': 0.24355481654697542,
'__contains__:False': 0.24382793854783813,
'find:True': 0.3067379407923454,
'find:False': 0.29860888058124146,
'index:True': 0.29647137792585454,
'index:False': 0.5502287584545229}
पायथन स्ट्रिंग्स और सूचियों में
यहां कुछ उपयोगी उदाहरण दिए गए हैं जो विधि in
संबंधित स्वयं के लिए बोलते हैं:
"foo" in "foobar"
True
"foo" in "Foobar"
False
"foo" in "Foobar".lower()
True
"foo".capitalize() in "Foobar"
True
"foo" in ["bar", "foo", "foobar"]
True
"foo" in ["fo", "o", "foobar"]
False
चेतावनी। सूचियां पुनरावृत्त हैं, और विधि केवल तारों पर ही नहीं, बल्कि तारों पर कार्य करती है।
आप ऑपरेटर का उपयोग कर सकते हैं:
if "blah" not in somestring:
continue
आपका जवाब यहां दिया गया है:
if "insert_char_or_string_here" in "insert_string_to_search_here":
#DOSTUFF
यह जांचने के लिए कि क्या यह गलत है:
if not "insert_char_or_string_here" in "insert_string_to_search_here":
#DOSTUFF
या:
if "insert_char_or_string_here" not in "insert_string_to_search_here":
#DOSTUFF
नहीं, कोई string.contains(str)
विधि नहीं है, लेकिन ऑपरेटर में है:
if substring in someString:
print "It's there!!!"
यहां एक जटिल कामकाजी उदाहरण है:
# Print all files with dot in home directory
import commands
(st, output) = commands.getstatusoutput('ls -a ~')
print [f for f in output.split('\n') if '.' in f ]
पायथन में आप इसे प्राप्त कर सकते हैं दो सरल तरीके हैं:
पाइथोनिक तरीका: पाइथन के 'इन' कीवर्ड का उपयोग करना-
दो "तर्क" लेते हैं, बाएं ( सबस्ट्रिंग ) पर एक और दाईं ओर एक, और दाएं तर्क को अधिकार के तर्क में निहित है और यदि नहीं, तो यह False
।
example_string = "This is an example string"
substring = "example"
print(substring in example_string)
आउटपुट:
True
गैर पायथनिक तरीका: पायथन के str.find का उपयोग करना:
find
विधि स्ट्रिंग या -1 के भीतर स्ट्रिंग की स्थिति देता है यदि यह नहीं मिला है। लेकिन बस जांचें कि स्थिति -1 नहीं है या नहीं।
if example_string.find(substring) != -1:
print('Substring found!')
else:
print('Substring not found!')
आउटपुट:
Substring found!
यदि आप "blah" in somestring
लेकिन यह एक समारोह कॉल होना चाहते हैं, तो आप शायद यह कर सकते हैं
import operator
if not operator.contains(somestring, "blah"):
continue
पाइथन में सभी ऑपरेटरों को ऑपरेटर मॉड्यूल में कम या ज्यादा पाया जा सकता in
।
यदि आप किसी अन्य शब्द के भीतर निहित एक सबस्ट्रिंग के बजाय पूरे शब्दों के लिए केस-असंवेदनशील खोज की तलाश में हैं:
import string
s = 'This is my text example'
if 'is' not in (word.lower()
for split_char in string.punctuation + string.whitespace
for word in s.split(split_char)):
# do something
यह जानने का एक और तरीका है कि स्ट्रिंग में कुछ वर्ण हैं या नहीं, बूलियन रिटर्न वैल्यू (यानी True
या 'गलत):
str1 = "This be a string"
find_this = "tr"
if find_this in str1:
print find_this, " is been found in ", str1
else:
print find_this, " is not found in ", str1
if needle in haystack:
सामान्य उपयोग है, जैसा कि @ माइकल कहते हैं - यह ऑपरेटर पर निर्भर करता है, एक विधि कॉल से अधिक पठनीय और तेज़।
यदि आपको वास्तव में ऑपरेटर की बजाय एक विधि की आवश्यकता है (उदाहरण के लिए कुछ अजीब key=
एक बहुत ही असाधारण प्रकार के लिए ...?), यह 'haystack'.__contains__
। लेकिन चूंकि आपका उदाहरण किसी उपयोग में उपयोग के लिए है, if
मुझे लगता है कि आप वास्तव में इसका मतलब नहीं है कि आप क्या कहते हैं ;-)। यह विशेष रूप से विशेष तरीकों का उपयोग करने के लिए अच्छा फॉर्म नहीं है (न ही पठनीय, और न ही कुशल) - इसका उपयोग उन ऑपरेटरों और बिल्टिन के माध्यम से किया जाता है, जो उनके लिए प्रतिनिधि हैं।