AI工具王炸组合:Claude+Codex+Grok三模型协作实战
2026/10/10 13:08:00
not in是 Python成员运算符,判断元素不在容器里面,返回布尔值True/False。
语法: 元素 not in 容器 |||| 元素 in 容器
容器可以是:列表、字符串、元组、集合、字典等
##1. 列表示例lst=[1,2,3,4]print(5notinlst)# True,5不在列表中print(2notinlst)# False,2在列表中##2. 字符串判断子串s="hello world"print("java"notins)# Trueprint("hello"notins)# False##3. 集合s={"apple","banana"}print("orange"notins)# True##4.字典:in / not in 只判断键 (key),不判断值d={"name":"Tom","age":18}print("name"notind)# Falseprint("Tom"notind)# True Tom是值,不是键##常用搭配 if 判断(最实用)words=["a","b","c"]x="d"ifxnotinwords:print("x 不在列表里面")##反面等价写法(不推荐)ifnot(xinwords):效果和 `xnotinwords` 一模一样,但是可读性差,优先用 `notin`。## 实战小例子:去重添加元素data=[]item=10ifitemnotindata:data.append(item)