使用 in 运算符 python 无法在另一个字符串中找到来自正则表达式的字符串 return

String return from regex can not be found in another string using in operator python

我目前正在学习正则表达式,但我很难调试。我的目标是给定一个包含多个比率或类似格式的比率的字符串,提取格式为 x+:x+(+ 表示多个数字)的所有正确比率。这是我的代码:

string= "2890.1:2004 1.45.7 2890.6:2009 505.204:908.890 1:100 0.55:1 10:59:40"

#empty string declaration
other_str = str()
time_str = str()
ratio_str = str()

#pattern for ratio, time and other format
pattern_ratios = re.compile(r'1:[-+]?[0-9]+')
pattern_time = re.compile(r'[0-9]+:[0-9]+:[0-9]+')
pattern_other = re.compile(r'\d*\.?\d+:\d*\.?\d+')

#create irritable re.match object
matches_ratios = pattern_ratios.finditer(string)
matches_time = pattern_time.finditer(string)
matches_other = pattern_other.finditer(string)

#create a time string to store all time format found
for time_match in matches_time:
    time_str += string[time_match.span()[0]:time_match.span()[1]] + ' '
print('time string =',time_str)

#create a other string to store all other format found
for other_match in matches_other:
    other_str += string[other_match.span()[0]:other_match.span()[1]] + ' '
print('other string =', other_str)

#create a ratio string to store all ratio format found
for ratio_match in matches_ratios:
    ratio = string[ratio_match.span()[0]:ratio_match.span()[1]]
    print('\nratio =',ratio)
    print('not in other string:',ratio not in other_str)
    print('not in time string:',ratio not in time_str)
    if (ratio not in other_str and ratio not in time_str):
        ratio_str += ratio + ' '

print('ratio list =',ratio_str.split())

输出为:

time string = 10:59:40
other string = 2890.1:2004 2890.6:2009 505.204:908.890 1:100 0.55:1 10:59

ratio = 1:2004
not in other string: False

not in time string: True

ratio = 1:100
not in other string: False

not in time string: True

ratio list= []

这是一个意外的输出,因为根据我的理解,如果我在 2 个不同的字符串中执行此操作,如下所示:

str1 = '2890.1:2004 2890.6:2009 505.204:908.890 1:100 0.55:1 10:59'
str2 = '1:2004'
str2 in str1

输出为真! 是不是和in-operator本身有关?

问题是您在应该使用 or:

的地方使用了 and
if (ratio not in other_str or ratio not in time_str):
        ratio_str += ratio + ' '

这应该会给你结果:

ratio list = ['1:2004', '1:100']