เมื่อต้องการตรวจสอบว่ามีสมาชิกของรายการอยู่ใน Tuple หรือไม่ จะใช้ค่า Boolean และการวนลูปแบบง่าย
ตัวอย่าง
my_tuple = (14, 35, 27, 99, 23, 89, 11)
print("The tuple is :")
print(my_tuple)
my_list = [16, 27, 88, 99]
print("The list is :")
print(my_list)
my_result = False
for element in my_list:
if element in my_tuple:
my_result = True
break
print("The result is :")
if(my_result == True):
print("The element is present in the tuple")
else:
print("The element isn't present in the tuple")
ผลลัพธ์
The tuple is : (14, 35, 27, 99, 23, 89, 11) The list is : [16, 27, 88, 99] The result is : The element is present in the tuple
คำอธิบาย
กำหนด Tuple ของจำนวนเต็มและแสดงผล
กำหนดรายการของจำนวนเต็มและแสดงผล
กำหนดค่า Boolean เริ่มต้นเป็น
Falseวนลูปผ่านรายการ หากพบสมาชิกอยู่ใน Tuple จะตั้งค่าเป็น
Trueและออกจากลูปแสดงผลตามค่า Boolean