首页 > 解决方案 > 竞争性编程:如何检查变量的值是否存在于 python 列表中?

问题描述

在我的一项能力倾向测试中提出了以下问题。我尝试使用 python 解决。问题如下:

   COVID19 Patients are with identity numbers from 1 to 10. 
But few patients are escaped from quarantine. 
Find out those patients and write their names and details.
 Below are details of patients.

 1 Raju 
2 Ravi 
3 Lakshmi 
4 Yash 
5 Renu 
6 Tanvi 
7 Usha 
8 Vicky 
9 Rocky

10 Manu

输出和输入的格式如下:

Input Format

Line 1: String
Line 2: String 
Line 3: String 
Line 4: String 
Line 5: String 
Line 6: String 
Line 7: String 
Line 8: String
Line 9: String


Constraints

None

Output Format

Line 1: String

示例输入和输出如下所示:

Sample Input 0

Raju
Ravi 
Lakshmi
Yash
Tanvi
Usha
Vicky
Rocky 
Manu

Sample Output 0

Renu

因此,我将所有名称都赋予了一个名为 Patient[] 的数组。我的问题程序如下:

patient=[
"Raju",
"Ravi",
"Lakshmi",
"Yash",
"Renu",
"Tanvi",
"Usha",
"Vicky",
"Rocky",
"Manu"
]

for i in range(0,):
item=input()
if item not in patient:
    print(item)

标签: pythonlistconditional-statements

解决方案


您不能迭代 arange(0,)因为没有定义端点,它必须是range(0,len(patient))

那么这就是你的循环的样子:

for i in range(0,len(patient)):
    item=input()
    if item not in patient:
        print(item)

推荐阅读