gpt4 book ai didi

python - 如何仅在 f1 匹配时打印 else

转载 作者:行者123 更新时间:2023-12-05 08:10:39 25 4
gpt4 key购买 nike

下面的正则表达式目前运行良好。如果 f1 和 f2 或 f3 匹配,它将返回。

我需要帮助弄清楚如何仅在 f1 匹配时打印 else,例如print("哪个城市,哪个月份?)

import re

string = ['I am looking to rent a home in Los Angeles']

for s in string:
f1 = re.findall(r'(.{0,50}\b(home|house|condo)\b.{0,50})',s, re.IGNORECASE)
if f1:

f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f2:
print("For what month?")

f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f3:
print("For what city?")```

最佳答案

确定 f1 匹配后,收集 f2f3 匹配。然后检查 f2f3 是否匹配,否则,执行 f1 block :

import re

string = ['I am looking to rent a home in Los Angeles']

for s in string:
f1 = re.search(r'.{0,50}\b(home|house|condo)\b.{0,50}',s, re.IGNORECASE)
if f1:
f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})', f1.group(), re.IGNORECASE)
f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})', f1.group(), re.IGNORECASE)
if f2:
print("For what month?")
elif f3:
print("For what city?")
else:
print("Only f1 fired, not f2 and f3")
else:
print("No f1!")

关于python - 如何仅在 f1 匹配时打印 else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71913680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com