集計処理
リストやmapなどである要素が何回ずつ出現するかカウントする処理。
集約処理の他の呼び方、頻度、キーカウント、古い値に基づいた値の更新など
https://gyazo.com/3f32b6db10645243f9f7efb03ecac378
C++
code:C++
using namespace std;
template <typename K, typename V, typename A>
void map_count (map<K, V> &mapBuff, A a){
for(auto c : a) {
if (mapBuff.count(c) == 0 ) {
mapBuff.insert(make_pair(c, 1));
} else {
auto it = mapBuff.find(c);
mapBuff.insert(make_pair(c, it->second++));
}
}
}
Rust
code:rust
fn main() {
use std::collections::HashMap;
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
}
Python
code:python
import collections
c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})
print(type(c))
# <class 'collections.Counter'>
print(issubclass(type(c), dict))
# True
print(l.count('a'))
# 4
print(l.count('b'))
# 1
print(l.count('c'))
# 2
print(l.count('d'))
# 0
code:python
S = input()
aggregate = {}
for c in S:
if (c in aggregate) == False:
else:
print(aggregate)