正则表达式:在列表中搜索 [英] Regular Expressions: Search in list

查看:78
本文介绍了正则表达式:在列表中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据正则表达式过滤列表中的字符串.

I want to filter strings in a list based on a regular expression.

有什么比 [x for x in list if r.match(x)] 更好的吗?

Is there something better than [x for x in list if r.match(x)] ?

推荐答案

您可以通过以下方式在 Python 3.x 中创建迭代器或在 Python 2.x 中创建列表使用:

You can create an iterator in Python 3.x or a list in Python 2.x by using:

filter(r.match, list)

要将 Python 3.x 迭代器 转换为列表,只需将其强制转换即可;list(filter(..)).

To convert the Python 3.x iterator to a list, simply cast it; list(filter(..)).

这篇关于正则表达式:在列表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆