word="hello"
word_list="hello world"
if word in word_list:
print("True")
else:
print("False")
result:True
word="hello"
word_list=["hello world","today is sunny","happy new year"]
if word in word_list:
print("True")
else:
print("False")
# result:False
word="hello"
word_list=["hello","today is sunny","happy new year"]
if word in word_list:
print("True")
else:
print("False")
# result:True
结论: in 字符串匹配时,为部分匹配 in 列表匹配时,为完全匹配
如何对列表中的对象进行部分匹配呢
word="hello"
word_list=["hello world","today is sunny","happy new year"]
# 方案1
result=[]
for text in str1:
if str in text:
result.append(text)
# 方案2
result = [v for v in word_list if word in v]
# 方案3
result=list(filter(lambda x: word in x, word_list))
#大小写转换
l = list(map(str.lower, l)) 映射字符串列表为小写
word1=word.lower(),word1小写 但word不变
# result:a=["hello world"]
|