集計処理
リストやmapなどである要素が何回ずつ出現するかカウントする処理。
集約処理の他の呼び方、頻度、キーカウント、古い値に基づいた値の更新など
https://gyazo.com/3f32b6db10645243f9f7efb03ecac378
https://twitter.com/chokudai/status/1255780070410448899?ref_src=twsrc^tfw|twcamp^tweetembed|twterm^1255780070410448899|twgr^&ref_url=https%3A%2F%2Fwww.yu2ta7ka-emdded.com%2Fentry%2F2020%2F04%2F30%2F200442
C++
code:C++
#include <map>
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
https://doc.rust-jp.rs/book/second-edition/ch08-03-hash-maps.html#a古い値に基づいて値を更新する
code:rust
#!allow(unused_variables)
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
https://note.nkmk.me/python-collections-counter/
code:python
import collections
l = 'a', 'a', 'a', 'a', 'b', 'c', 'c'
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:
aggregatec = 1
else:
aggregatec += 1
print(aggregate)
https://www.yu2ta7ka-emdded.com/entry/2020/04/30/200442