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

วิธีเช็คว่ามี String ใดใน Array อยู่ใน String อื่นใน Python

ใช้ any() ร่วมกับ generator expression

ตัวอย่าง

arr = ['a', 'e', 'i', 'o', 'u']
str = "hello people"
if any(c in str for c in arr):
    print("Found a match")

ผลลัพธ์

Found a match

วิธี Regex (overkill แต่ได้ผล)

import re
arr = ['a', 'e', 'i', 'o', 'u']
str = "hello people"
if any(re.findall('|'.join(arr), str)):
    print('Found a match')