python - 如何在没有库的情况下创建计数器?
问题描述
我想建立一个计数器(仅使用“纯”python --> 没有库),它带有countries_nametags 列表,它将在字典中识别它并计算与该country_nametag关联的大陆。
例子:
continent_dict = {'BR': 'America','GEO': 'Asia','JP': 'Asia','SWZ': 'Africa','GER':'Europe','SRB': 'Europe','ARG': 'America'}
列表:
countries = ['PT','ARG','GEO','JP','SRB','BR']
continent_counter = {'America':0, 'Asia':0,'Africa':0,'Europe':0,'Oceania':0} #dictionary that stores the number of occurencies
for country in countries:
continent_counter[continent_nations_dict[country]] = 1
Max_Continent = max(continent_counter, continent_counter.get)
它得到错误......(可能以我获得max_continent 值的方式?)
解决方案
您可以将数组值“映射”为字典中的大陆列表,并使用“max”计算最频繁的大陆
c_list=list(countries.map(lambda x: continent_dict[x])
max( set(c_list), key=c_list.count)
推荐阅读
- java - 如何使用Java字符串replaceAll将字符串中所有出现的“替换为\”
- python - 在 docker 上运行 django+nginx+celery+gunicorn+daphne
- git - 我们能否提供对文件的分支级别访问权限,即甚至可以在 codecommit 门户中查看文件
- http - 通过http在Arduino ESP32上解决文件上传问题的任何解决方案
- emulation - 如何使用 zcutlip 的 nvram-faker
- java - Spring 5:NoSuchBeanDefinitionException 没有 IIdPartenaireInteropCsnBS 类型的合格 bean
- javascript - Ant Design Charts 中的饼图没有按百分比显示标签的选项。我该如何手动完成?
- audio - 使用 librosa 时如何防止内存泄漏?
- google-apps-script - 在 Google 驱动器中移动文件导致 getFilesByName 不是函数错误
- linux - 如何在 vvar.h 中为我的新 VSDO 函数正确创建一个新变量?