问题:
假设我有一个列表 a = ['abd', 'the dog', '4:45 AM', '1234 total', 'etc...','6:31 PM', '2:36 ']
如何删除 4:45 AM
和 6:31 PM
和“2:36”等元素?即,如何删除 number:number|number
形式的元素以及末尾带有 AM/PM 的元素?
说实话,我没有尝试太多,因为我不确定从哪里开始,除了类似的东西:
[x for x in a if x != something]
最佳答案
您可以使用正则表达式 \d+(?::\d+)?$
并使用它进行过滤。
查看演示。
https://regex101.com/r/HoGZYh/1
import re
a = ['abd', ' the dog', '4:45', '1234 total', '123', '6:31']
print [i for i in a if not re.match(r"\d+(?::\d+)?$", i)]
输出:['abd', 'the dog', '1234 total']
https://stackoverflow.com/questions/53659253/