Python regular expression to filter list of strings matching a pattern
问题 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" But how to do it in python if test is a list? P.S. I am learning python using google's python exercise. And I prefer using regression expression. 回答1: 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 回答2: You could