MST

星途 面试题库

面试题:Python中择一匹配符号的常见应用场景

在Python中,假设你有一个字符串列表,列表中的元素可能是'cat'或者'dog',请使用择一匹配符号(比如在正则表达式的情境下),编写代码统计列表中'cat'和'dog'出现的总次数。
22.2万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
string_list = ['cat', 'dog', 'cat', 'dog']
import re
total_count = len(re.findall(r'cat|dog', ' '.join(string_list)))
print(total_count)