在python中搜索单个字符串中的字符串

时间:2013-11-27 09:36:41

标签: python

在这段代码中,如何在单个语句中检查单个字符串中的多个字符串?

line = '{        AutoLogin = 0;        Captive = 0;        Closed = 0;        Disabled = 0;        LastConnected = "2013-11-27 08:38:10 +0000";        Passpoint = 0;        PossiblyHiddenNetwork = 0;        SPRoaming = 0;        SSID = <534253>;        SSIDString = SBS;        SecurityType = "WPA/WPA2 Personal";        SystemMode = 1;        TemporarilyDisabled = 0;    })'

for token in line.split( ';' ):
    if 'RecentNetworks' in token:
        start = True
    if 'LastConnected' in token:
        start = True

2 个答案:

答案 0 :(得分:3)

试试这个:

for token in line.split(';'):
    start = any(s in token for s in ["RecentNetworks", "LastConnected"])

答案 1 :(得分:0)

您可以使用正则表达式:

if re.search(r'RecentNetworks|LastConnected', token):
    start = True

我不确定这是否比any解决方案更快,但我认为它。