即使用户输入的内容为小写或大写字母,我也想删除该元素

时间:2019-09-19 11:57:46

标签: python-3.x list

nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
name.remove(nametoRemove)
print(name)

1 个答案:

答案 0 :(得分:0)

您可以使用for循环和if语句进行基本设置

nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
for item in name:
    if item.lower() == nametoRemove.lower():
        name.remove(item)
print(name)

输出:

Enter the name you want to remove:ramesh
ramesh
['Rakesh', 'Suresh']