एक सूची में इसके तत्वों के रूप में टुपल्स हो सकते हैं। इस लेख में हम सीखेंगे कि उन टुपल्स की पहचान कैसे करें जिनमें एक विशिष्ट खोज तत्व होता है जो एक स्ट्रिंग है।
इन और कंडीशन के साथ
हम कंडीशन के साथ फॉलो डिजाइन कर सकते हैं। के बाद हम स्थिति या शर्तों के संयोजन का उल्लेख कर सकते हैं।
उदाहरण
listA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
test_elem = 'Mon'
#Given list
print("Given list:\n",listA)
print("Check value:\n",test_elem)
# Uisng for and if
res = [item for item in listA
if item[0] == test_elem and item[1] >= 2]
# printing res
print("The tuples satisfying the conditions:\n ",res) आउटपुट
उपरोक्त कोड को चलाने से हमें निम्नलिखित परिणाम मिलते हैं -
Given list:
[('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
Check value:
Mon
The tuples satisfying the conditions:
[('Mon', 3), ('Mon', 2)] फ़िल्टर के साथ
हम लैम्ब्डा फ़ंक्शन के साथ फ़िल्टर फ़ंक्शन का उपयोग करते हैं। फ़िल्टर स्थिति में हम टपल में तत्व की उपस्थिति की जांच करने के लिए इन ऑपरेटर का उपयोग करते हैं।
उदाहरण
listA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
test_elem = 'Mon'
#Given list
print("Given list:\n",listA)
print("Check value:\n",test_elem)
# Uisng lambda and in
res = list(filter(lambda x:test_elem in x, listA))
# printing res
print("The tuples satisfying the conditions:\n ",res) आउटपुट
उपरोक्त कोड को चलाने से हमें निम्नलिखित परिणाम मिलते हैं -
Given list:
[('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
Check value:
Mon
The tuples satisfying the conditions:
[('Mon', 3), ('Mon', 2)]