python - 元素/值的计数出现在列表列表中
问题描述
如何计算元素/值在列表列表中出现的次数?
我想返回测试发生的次数。
my_list = [['test', 'idk', 'blah'], ['test', 'idk', 'blah'], ['test', 'idk', 'blah'], ['test', 'idk', 'blah'], ['test', 'idk', 'blah']]
print(my_list.count('test'))
>>> 0
所需输出:5
其次,我只想检查/计数测试每个列表的第一个元素,例如my_list[0/1/2/...][0]
解决方案
试试这个:
print(sum(x.count('test') for x in my_list))
# 5
推荐阅读
- javascript - 如何添加内容安全策略 (CSP)
- javascript - Node.js 中的多个 setTimeout 执行是单线程还是多线程?
- postgresql - 如何将从.bat文件中的复制命令返回的错误插入到表中
- spring-boot - 我们如何更改默认的 Spring Boot 登录页面
- nginx - 如何在 nginx 中将子域代理到主域以进行 SEO
- swagger - 为什么最近大幅减少了 swagger jbpm 文档?
- mongodb - 将备份从容器导出到主机
- python - 如何在不发生事件的情况下结束 SSEClient?
- php - mvc网站中的静态和非静态方法
- c - Linux C Programming,尝试写:实现FILE系统模仿