如何从列表中删除所有符合特定格式的字符串?

cjg*_*123 7 python formatting python-2.7

问题:说我有一个清单 a = ['abd', ' the dog', '4:45 AM', '1234 total', 'etc...','6:31 PM', '2:36']

我该如何去删除元素如4:45 AM6:31 PM和"2:36"?即,如何删除表单元素number:number|number和末尾AM/PM 元素?

说实话,我没有多尝试,因为我不确定到底哪里开始,除了以下内容:

[x for x in a if x != something]
Run Code Online (Sandbox Code Playgroud)

vks*_*vks 11

您可以使用正则表达式\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)]
Run Code Online (Sandbox Code Playgroud)

输出: ['abd', ' the dog', '1234 total']