python .count 用于多维数组(列表列表)

标签 python

如何计算嵌套列表构成的多维数组中某个值出现的次数?如在以下列表中查找“foobar”时:

list = [['foobar', 'a', 'b'], ['x', 'c'], ['y', 'd', 'e', 'foobar'], ['z', 'f']]

它应该返回 2

(是的,我知道我可以编写一个只搜索所有内容的循环,但我不喜欢该解决方案,因为它相当耗时(在运行时编写))

.也许算数?

最佳答案

>>> list = [['foobar', 'a', 'b'], ['x', 'c'], ['y', 'd', 'e', 'foobar'], ['z', 'f']]
>>> sum(x.count('foobar') for x in list)
2

关于python .count 用于多维数组(列表列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17718271/

相关文章:

python - OperatorNotAllowedInGraphError : using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. 添加指标到编译函数

python - 为什么覆盖 resetful JSONWebTokenSerializer 只保持返回 token ? Python

javascript - Django的简单文件浏览器

python - python PIL 可以处理原始图像数据吗?

python - 如何在抓取请求时绕过 Google Recaptcha

php - 检查域名是否被注册

python - 在 Python 中验证字典

python - 在 python 脚本中将参数传递给 scrapy spider

python - 我需要使用 pylast 的 library.add_album 功能的帮助(python last.fm api 包装器)

python - in line.split ('+' )[-1] 方括号中的-1在Python中表示什么