---
讲解对象:ModuleNotFoundError: No module named 'collections.Counter'
作者:融水公子 rsgz
Python3教程
先开始的时候是这样import的,然后报错了。#!/usr/bin/python3
分析:collections模块是我们的标准库。每个下载好的python没有损坏的情况下都会自带这个库。那么为什么不能用?Counter叫做计数器,本质上是一个函数,不能被import.,只能下面的形式import.
import collections.Counter as len1
list3 = ["唐三", "牧尘", "林动", "萧炎"]
print(len1(list3))
报错:ModuleNotFoundError: No module named 'collections.Counter'import collections
正确运行一遍#!/usr/bin/python3
---
import collections
list3 = ["唐三", "牧尘", "林动", "萧炎"]
print(collections.Counter(list3))
# Counter({'唐三': 1, '牧尘': 1, '林动': 1, '萧炎': 1})
联系客服