मान लें कि मैं कच्चे इनपुट के लिए उपयोगकर्ता से पूछता हूं और उन्होंने कहा, "यह एक संदेश है।" यदि उस कच्चे इनपुट में शब्द "संदेश" होता है तो उसके बाद यह कार्रवाई करेगा। क्या मैं देख सकता हूं कि यह कैसे किया जा सकता है?मैं कैसे जांचूं कि वाक्य में पायथन में एक निश्चित शब्द है और फिर कोई क्रिया करें?
if "message" in raw_input():
action()
आप विभिन्न कार्यों के लिए अलग अलग शब्द मैप करने के लिए आवश्यक हैं, तो आप कुछ इस तरह कर सकता है::
# actions
def action():
print "action"
def other_action():
print "other action"
def default_action():
print "default action"
# word to action translation function
def word_to_action(word):
return {
"message": action,
"sentence": other_action
}.get(word, default_action)()
# get input, split into single words
w = raw_input("Input: ").split()
# apply the word to action translation to every word and act accordingly
map(word_to_action, w)
आप सामग्री फ़िल्टर बनाने की कोशिश करते हैं, तो इस पर विचार करें: शब्द "क्लासिक" उस में "गधा" है (। जो, Btw गधा के लिए एक और शब्द है) । यह निश्चित रूप से केवल एक नमूना है- – knitti