(यह सिर्फ एक उदाहरण है जिसे मैं हल करना चाहता हूं)
array = ["hello", "hi"]
statement = input()
condition = any(statement in elm for elm in array)
क्या उस तत्व की अनुक्रमणिका वापस करने का कोई तरीका है जो सत्य लौटाता है, या क्या मुझे केवल लूप के लिए उपयोग करना चाहिए?
2 जवाब
वास्तव में आप array
में कथन का index
चाहते हैं
array = ["hello", "hi"]
statement = input("Give a word: ")
condition = array.index(statement) if statement in array else -1
print(condition)
डेमो
Give a word: hello
0
Give a word: hi
1
Give a word: Hi
-1
अगली बार फ़ंक्शन गुण खोजें। मैं पायथन दस्तावेज़ों पर उदाहरण कोड आधार लिखता हूं
array = ["hello", "hi", "example", "to get", "index from array"]
def SearchIndex():
statement = input()
#// Check if input is item on list
if statement in array: print(">>", array.index(statement))
#// Other search if input is part of item in list
else:
part_of = False
for item in array:
if statement in item:
print('>> {0} is part of "{2}" -> {1}'.format(statement, array.index(item), array[array.index(item)]))
#// If founded signal
part_of = True
#// If nout found (signal is false)
if part_of == False: print(f">> {statement} is not on list...")
while 1:
SearchIndex()
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।