Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี grep คียーワードเฉพาะจาก Python Tuple

หากคุณมี tuple ของสตริงและต้องการค้นหาสตริงเฉพาะ คุณสามารถใช้ออเปอเรเตอร์ in ได้

ตัวอย่าง

tpl = ("Hello", "world", "Foo", "bar")
print("world" in tpl)

ผลลัพธ์

True

ตัวอย่าง

หากคุณต้องการตรวจสอบว่ามี substring อยู่หรือไม่ คุณสามารถวนลูปผ่าน tuple และหาได้โดยใช้:

tpl = ("Hello", "world", "Foo", "bar")
for i in tpl:
    if "orld" in i:
        print("Found orld in " + i)

ผลลัพธ์

Found orld in world