如何检查特定整数是否在列表中 [英] How to check if a specific integer is in a list

查看:21
本文介绍了如何检查特定整数是否在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何制作一个 if 语句,如果某个整数在列表中,则执行一个子句.

I want to know how to make an if statement that executes a clause if a certain integer is in a list.

我看到的所有其他答案都要求特定条件,例如素数、重复数等,但我无法从其他答案中收集到我的问题的解决方案.

All the other answers I've seen ask for a specific condition like prime numbers, duplicates, etc. and I could not glean the solution to my problem from the others.

推荐答案

您可以简单地使用 in 关键字.像这样:

You could simply use the in keyword. Like this :

if number_you_are_looking_for in list:
    # your code here

例如:

myList = [1,2,3,4,5]

if 3 in myList:
    print("3 is present")

这篇关于如何检查特定整数是否在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆