gpt4 book ai didi

Python 组合条件运算符 : if AA in Y or BB in Y and not B in Y

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:48 29 4
gpt4 key购买 nike

我想将此代码运行为“if 'Britain' or 'UK' in string do stuff, but don't do stuff if "Ex UK" 在字符串中":

#Case insensitive wrapper - equivalent to if X in Y
def case_in(phrase, string):
return phrase.lower() in string.lower()

name = "Developed Europe Ex UK Property"

#This is the relevant part
if case_in("Britain", name) or case_in(" UK ", name) and not case_in("Ex UK", name):
geofocusregion = "Europe"
geofocuscountry = "GB"
fundcurrency = "GBP"

但是,对于包含 "Ex UK" 的字符串,它仍然会设置变量,例如geofocuscountry = GB

最佳答案

在 python 中 the precedence and 高于 or 因此您需要将前 2 个条件放在括号内。

if (case_in("Britain", name) or case_in(" UK ", name)) and not case_in("Ex UK", name):

关于Python 组合条件运算符 : if AA in Y or BB in Y and not B in Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31160430/

29 4 0
文章推荐: java - Tapestry5 使用 hashmap 显示网格组件
文章推荐: html - thymeleaf 里面
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com