完结了!

这个系列反响还不错,所以一口气把 collections 模块下面几个类都写完了。这篇算完结篇,希望对你有帮助。

Counter

Counter 也是 dict 的子类,dict 有的特性它都有,正如其名,它的主要作用是用于统计计数。

比如统计一个字符串中每个单词出现的次数,统计一个列表中每个元素出现的次数等等。凡是需要计数的场景都可以用Counter来实现。

例如我要统计字符串”hello world” 中每个单词的出现次数时,用字典来实现可以这样。

>>> c = dict()
>>> for s in "helloworld":
...     c[s] = c.setdefault(s, 0) + 1

>>> c
{'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}

如果使用 Counter,就更简单了,直接将字符串作为参数传进去就好。

>>> from collections import Counter
>>> c = Counter("helloworld")
>>> c
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1})

Counter 还有一个比较实用的方法是 Counter.most_common(n), 它可以返回前n个出现次数最多的元素

>>> c.most_common(1)  # 出现次数最多的前1位
[('l', 3)]
>>> c.most_common(2)   # 出现次数最多的前2位
[('l', 3), ('o', 2)]
>>> c.most_common(3)  # 出现次数最多的前3位
[('l', 3), ('o', 2), ('h', 1)]
>>>

这个在统计排行时有用,不过在大型应用中,如果数据是保存在数据库的,这种操作其实在数据库来实现就行,没必要用Python来处理。

OrderedDict

OrderedDict 同样是 dict 的子类,它是一个有序的字典,在Python3.6以前,字典元素并不保证有序,所以就有OrderedDict。

>>> from collections import OrderedDict
>>> od = OrderedDict()
>>> od['a'] = 1
>>> od['b'] = 2
>>> od['c'] = 3
>>> od
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

所谓有序不是按照大小来排,而是按照key的加入顺序排列,a 最先加入,所以排在前面。但是,从Python3.7开始,dict 本身也拥有了这种特性,例如:

>>> d = dict()
>>> d['a'] = 1
>>> d['b'] = 2
>>> d['c'] = 3
>>> d
{'a': 1, 'b': 2, 'c': 3}

默认按照插入的顺序排的,所以,从Python3.7开始,OrderedDict的用处就并不大了,因为 dict 已经实现了有序这个特性。

推荐:

  1. Python最冷门的模块

  2. python中最高频的模块

关注我,每天分享实用小知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,我了解了你的问题。以下是2019BJFU C++实验习题的题目和解答: 1. 编写一个程序,输入一个整数n,输出n的阶乘。 代码实现: ```c++ #include <iostream> using namespace std; int main() { int n; cin >> n; int result = 1; for (int i = 1; i <= n; i++) { result *= i; } cout << result << endl; return 0; } ``` 2. 编写一个程序,输入一个字符串,将其中的小写字母全部转换成大写字母,并输出转换后的字符串。 代码实现: ```c++ #include <iostream> #include <cstring> using namespace std; int main() { char str[100]; cin.getline(str, 100); int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] >= 'a' && str[i] <= 'z') { str[i] = str[i] - 'a' + 'A'; } } cout << str << endl; return 0; } ``` 3. 编写一个程序,输入一个字符串,将其中的所有数字字符相加,并输出和。 代码实现: ```c++ #include <iostream> #include <cstring> using namespace std; int main() { char str[100]; cin.getline(str, 100); int len = strlen(str); int sum = 0; for (int i = 0; i < len; i++) { if (str[i] >= '0' && str[i] <= '9') { sum += str[i] - '0'; } } cout << sum << endl; return 0; } ``` 4. 编写一个程序,输入一个整数n,输出所有小于n的正整数中,能被3或5整除的数的和。 代码实现: ```c++ #include <iostream> using namespace std; int main() { int n; cin >> n; int sum = 0; for (int i = 1; i < n; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } cout << sum << endl; return 0; } ``` 5. 编写一个程序,输入一个字符串,判断它是否为回文字符串,即正读和反读都一样(例如,"abcba"就是回文字符串)。 代码实现: ```c++ #include <iostream> #include <cstring> using namespace std; int main() { char str[100]; cin.getline(str, 100); int len = strlen(str); bool isPalindrome = true; for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - i - 1]) { isPalindrome = false; break; } } if (isPalindrome) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } ``` 希望这些代码能够帮助你解决问题。如果你还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值