我想基于正则表达式过滤列表中的字符串。
有比这更好的东西[x for x in list if r.match(x)]吗?
[x for x in list if r.match(x)]
您可以使用以下命令在Python 3.x中创建 迭代器 或在Python 2.x中创建 列表 :
filter(r.match, list)
要将Python 3.x 迭代器 转换为列表,只需对其进行转换即可;list(filter(..))。
list(filter(..))