繁体   English   中英

python等同于带有条件的“ grepl” R

[英]python equivalent to “grepl” R with condition

以下是R代码。 我可以知道与下面的代码等效的python吗? 特别是

grepl("dog|cat", data$animal)

部分代码

data <- data.frame(animal = sample(c("cat","dog","bird",'doggy','kittycat'), 
                                       10, replace = T)) 
ifelse(grepl("dog|cat", data$animal), "keep", "discard")

re用于正则表达式,遍历动物,并通过与None比较来测试是否存在匹配项:

>>> animal = ["cat","dog","doggy","kittycat","bird","dog"]
>>> import re
>>> [re.search("cat|dog",a) is not None for a in animal]
[True, True, True, True, False, True]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM