列出 "Looking and removing items in a list"
Lists "Looking and removing items in a list"
现在,我正在练习列表,所以我创建了 2 个列表,所以基本上我在其中一个列表中搜索不在第二个列表中的值,如果不存在,我想将其从列表中删除.但是结果并没有删除所有条目。
这是我所做的:
vocales = ["a","e","i","o","u"]
found = ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
for i in found:
if i not in vocales:
print(i, end=" ")
print(i not in vocales,end=" ")
found.remove(i)
print(found)
input("press enter to continue")
所以当在运行程序中时所有的辅音都不被识别
结果如下:
b True ['a', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
h True ['a', 'i', 'o', 'u', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
True ['a', 'i', 'o', 'u', 'l', 'c', 'm', 'e', 's', 't']
press enter to continue
m True ['a', 'i', 'o', 'u', 'l', 'c', 'e', 's', 't']
press enter to continue
s True ['a', 'i', 'o', 'u', 'l', 'c', 'e', 't']
press enter to continue
但是如果我 运行 代码没有从列表中删除任何值,它会识别所有字母。
vocales = ["a","e","i","o","u"]
found = ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
for i in found:
if i not in vocales:
print(i, end=" ")
print(i not in vocales,end=" ")
print(found)
input("press enter to continue")
这是结果:
b True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
h True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
l True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
c True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
m True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
s True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
t True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
所有辅音都被识别了,你能建议你如何删除没有出现在列表中的项目吗
使用列表理解。
found = [x for x in found if x not in vocales]
现在,我正在练习列表,所以我创建了 2 个列表,所以基本上我在其中一个列表中搜索不在第二个列表中的值,如果不存在,我想将其从列表中删除.但是结果并没有删除所有条目。
这是我所做的:
vocales = ["a","e","i","o","u"]
found = ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
for i in found:
if i not in vocales:
print(i, end=" ")
print(i not in vocales,end=" ")
found.remove(i)
print(found)
input("press enter to continue")
所以当在运行程序中时所有的辅音都不被识别
结果如下:
b True ['a', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
h True ['a', 'i', 'o', 'u', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
True ['a', 'i', 'o', 'u', 'l', 'c', 'm', 'e', 's', 't']
press enter to continue
m True ['a', 'i', 'o', 'u', 'l', 'c', 'e', 's', 't']
press enter to continue
s True ['a', 'i', 'o', 'u', 'l', 'c', 'e', 't']
press enter to continue
但是如果我 运行 代码没有从列表中删除任何值,它会识别所有字母。
vocales = ["a","e","i","o","u"]
found = ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
for i in found:
if i not in vocales:
print(i, end=" ")
print(i not in vocales,end=" ")
print(found)
input("press enter to continue")
这是结果:
b True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
h True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
l True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
c True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
m True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
s True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
t True ['a', 'b', 'i', 'o', 'u', 'h', 'l', ' ', 'c', 'm', 'e', 's', 't']
press enter to continue
所有辅音都被识别了,你能建议你如何删除没有出现在列表中的项目吗
使用列表理解。
found = [x for x in found if x not in vocales]