且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相当于 R 的 grepl 的最简单的 python

更新时间:2022-01-25 05:00:30

您可以使用列表推导式:

You can use list comprehension:

strings = ["aString", "yetAnotherString", "evenAnotherOne"]

["String" in i for i in strings]
#Out[76]: [True, True, False]

或者使用re模块:

import re

[bool(re.search("String", i)) for i in strings]
#Out[77]: [True, True, False]

或者使用 Pandas(R 用户可能对此库感兴趣,使用类似"结构的数据框):

Or with Pandas (R user may be interested in this library, using a dataframe "similar" structure):

import pandas as pd

pd.Series(strings).str.contains('String').tolist()
#Out[78]: [True, True, False]