从列表中查找优选字符串

时间:2017-09-18 20:55:19

标签: python string python-3.x

如果没有Regular expression的帮助,python中是否可以从列表中找到特定字符串,无论它们是什么情况?我的意思是,它将通过特定搜索查找所有案例。

请看下面的脚本,看看我的意思:

container = ['meet me','Meet Me','Say Hi','say hi','MEET ME','SAY HI']
for item in container:
    if 'meet' in item:
        print(item)

结果我有:

meet me

结果我想:

meet me
Meet Me
MEET ME

5 个答案:

答案 0 :(得分:4)

就像评论中提到的Akavall一样。只需:

container = ['meet me','Meet Me','Say Hi','say hi','MEET ME','SAY HI']
for item in container:
    if 'meet' in item.lower():
        print(item)

这会将每个字符串变为小写。因此,每种类型的会面(MEET,MeeT,MeEt)等都将变成“满足”#34;这将满足您if声明的条件

相反,如果您愿意,可以if 'MEET' in item.upper()。以下是str.lower()

的文档

答案 1 :(得分:2)

加上我的两分钱,理解。

BLANK,Z,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A
Z,0:00,0:52,1:44,2:37,3:29,4:21,5:13,6:06,6:58,7:50,8:42,9:34,10:27,11:15,12:53,13:30
O,0:00,0:00,0:52,1:44,2:37,3:29,4:21,5:13,6:06,6:58,7:50,8:42,9:34,10:27,11:15,12:53
N,0:00,0:00,0:00,0:52,1:44,2:37,3:29,4:21,5:13,6:06,6:58,7:50,8:42,9:34,10:27,11:15
M,0:00,0:00,0:00,0:00,0:52,1:44,2:37,3:29,4:21,5:13,6:06,6:58,7:50,8:42,9:34,10:27

答案 2 :(得分:1)

您可以尝试以下方式:

container = ['meet me','Meet Me','Say Hi','say hi','MEET ME','SAY HI']
for item in map(lambda s: s.lower(), container):
    if 'meet' in item:
        print(item)

答案 3 :(得分:0)

执行item.lower()。这将忽略比较时的情况。

答案 4 :(得分:0)

只需设置item.lower()以忽略大小写,然后进行所需的匹配:

container = ['meet me','Meet Me','Say Hi','say hi','MEET ME','SAY HI']
for item in container:
    if 'meet' in item.lower():
        print(item)