Python正则表达式可过滤与模式匹配的字符串列表

编程入门 行业动态 更新时间:2024-10-27 10:25:18
本文介绍了Python正则表达式可过滤与模式匹配的字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用R的次数更多,而在R中使用起来更容易:

I use R a lot more and it is easier for me to do it in R:

> test <- c('bbb', 'ccc', 'axx', 'xzz', 'xaa') > test[grepl("^x",test)] [1] "xzz" "xaa"

但是如果test是列表,如何在python中执行呢?

But how to do it in python if test is a list?

P.S.我正在使用Google的python练习来学习python.而且我更喜欢使用回归表达式.

P.S. I am learning python using google's python exercise. And I prefer using regression expression.

推荐答案

您可以使用以下内容查找列表中的任何字符串是否以'x'

You can use the following to find if any of the strings in list starts with 'x'

>>> [e for e in test if e.startswith('x')] ['xzz', 'xaa'] >>> any(e.startswith('x') for e in test) True

更多推荐

Python正则表达式可过滤与模式匹配的字符串列表

本文发布于:2023-11-05 09:03:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   模式   列表   正则表达式   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!