资讯详情

python in 与 not in

📅 2026/10/10 12:18:38 | 华诺云谱 👁 阅读
python in 与 not in
not in是 Python成员运算符判断元素不在容器里面返回布尔值True/False。语法: 元素 not in 容器 |||| 元素 in 容器容器可以是列表、字符串、元组、集合、字典等##1. 列表示例lst[1,2,3,4]print(5notinlst)# True5不在列表中print(2notinlst)# False2在列表中##2. 字符串判断子串shello worldprint(javanotins)# Trueprint(hellonotins)# False##3. 集合s{apple,banana}print(orangenotins)# True##4.字典in / not in 只判断键 (key)不判断值d{name:Tom,age:18}print(namenotind)# Falseprint(Tomnotind)# True Tom是值不是键##常用搭配 if 判断最实用words[a,b,c]xdifxnotinwords:print(x 不在列表里面)##反面等价写法不推荐ifnot(xinwords):效果和 xnotinwords 一模一样但是可读性差优先用 notin。## 实战小例子去重添加元素data[]item10ifitemnotindata:data.append(item)
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑