在列表中,我想删除包含“ S”,“ /”,“ K”的元素。 但是我首先想到的代码不起作用。第二个代码有效。 有什么区别?
new_info = ['3,926', 'S3,244', '/', 'K682)', '517', 'S91', 'K426)', '986', '398', 'S298', 'K100)', '474', 'S318', 'K156)', '3,387', 'S2,779', '/', 'K608)', '479', 'S79', '/', 'K400)', '894', '350', 'S278', 'K72)', '394', 'S258', 'K136)', '3,331', 'S2,723', '/', 'K608)', '479', 'S79', '/', 'K400)', '838', '350', 'S278', 'K72)', '394', 'S258', 'K136)']
for m in new_info:
if 'S' in m:
new_info.remove(m)
if 'K' in m:
new_info.remove(m):
if '/' in m:
new_info.remove(m)
print(new_info)
"""output: ['3,926', '/', '517', 'K426)', '986', '398', 'K100)', '474', 'K156)', '3,387', '/', '479', '/', '894', '350', 'K72)', '394', 'K136)', '3,331', '/', '479', '/', '838', '350', 'K72)', '394', 'K136)']"""
for m in new_info:
if 'S' in m:
new_info.remove(m)
for n in new_info:
if 'K' in n:
new_info.remove(n)
for o in new_info:
if '/' in o:
new_info.remove(o)
print(new_info)
"""output:['3,926', '517', '986', '398', '474', '3,387', '479', '894', '350', '394', '3,331', '479', '838', '350', '394']"""