【发布时间】:2020-05-05 10:07:40
【问题描述】:
我有这个功能:
def find_regex(regex, text, opzione2= None, opzione3 = None):
lista = []
for x in text:
matches_prima = re.findall(regex, x)
matches_prima2 = []
matches_prima3 = []
if opzione2 is not None:
matches_prima2 = re.findall(opzione2, x)
if opzione3 is not None:
matches_prima3 = re.findall(opzione3, x)
lunghezza = len(matches_prima) + len(matches_prima2) + len(matches_prima3)
lista.append(lunghezza)
print(sum(lista))
假设我的文本是“我爱猫”,正则表达式是“猫”,结果将是一个。
我有
def pandas():
regex1 = re.compile(r"cat")
text1 = "I love cats"
find_regex(regex1, text1) #it returns 1
df = pd.DataFrame([find_regex(regex1, text1)])
print(df)
这行不通。正确的做法是什么?
【问题讨论】:
-
你的
find_regex函数中的return语句在哪里? -
我没有,是这个问题吗?
-
如果您仍需要帮助,请考虑执行以下操作:1) 提供您的意见。一个字符串?一份文件?他们看起来怎么样?要搜索的单词列表?它们应该是完整的单词吗?等 2)您需要什么确切的预期输出。否则,我们帮不了你。
标签: python python-3.x regex pandas dataframe