列表中查找元素时可以使用和 in 运算符。
是的,可以使用 in 运算符来查找列表中是否包含某个元素。例如:
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 is in the list")
if 6 not in my_list:
print("6 is not in the list")
输出:
3 is in the list
6 is not in the list
其中,in 运算符返回一个布尔值,表示元素是否在列表中。not in 运算符则返回相反的结果。
原文地址: http://www.cveoy.top/t/topic/ckF7 著作权归作者所有。请勿转载和采集!