python - 如何计算列表中的选票并将其存储在字典中以跟踪选票数量
问题描述
例子:
list = ["a voted b", "c voted b", "d voted e", "e voted skip", "something else"]
字典结果:
{"b":2, "e":1, "skip":1}
解决方案
from collections import defaultdict
mylist = ["a voted b", "c voted b", "d voted e", "e voted skip"]
votes = defaultdict(int)
for item in mylist:
votes[item.split()[-1]] += 1
推荐阅读
- css - React Native:是否有可能使组件及其所有子项消失?
- java - 资源未正确加载到 Oracle BI Web 服务上
- c++ - 我如何防止函数中有太多参数。C++
- python - 使用 Python 2.7.5 绘制简单图形
- python - 具有可变长度键的字典中的 DataFrame
- python - Python多处理apply_async:不能将列表列表作为参数传递?
- java - 未找到 Maven 依赖项 InteliJ Ultimate 2020
- docker - 在非root用户下启动docker容器
- python - 如何从单个应用程序路由返回多个响应:Flask (Python)
- firebase - firebase onSnapshot 在创建完成之前获取更新