use std::collections::HashMap;
fn main() {
let data = r#"outlook temperature humidity windy play
sunny hot high FALSE no
sunny hot high TRUE no
overcast hot high FALSE yes
rainy mild high FALSE yes
rainy cool normal FALSE yes
rainy cool normal TRUE no
overcast cool normal TRUE yes
sunny mild high FALSE no
sunny cool normal FALSE yes
rainy mild normal FALSE yes
sunny mild normal TRUE yes
overcast mild high TRUE yes
overcast hot normal FALSE yes
rainy mild high TRUE no"#;
let lines: Vec<&str> = data.lines().collect();
let headers: Vec<&str> = lines[0].split('\t').collect();
let mut counts: HashMap<&str, HashMap<&str, usize>> = HashMap::new();
for &header in &headers {
counts.insert(header, HashMap::new());
}
for line in &lines[1..] {
let fields: Vec<&str> = line.split('\t').collect();
for (i, val) in fields.iter().enumerate() {
let map = counts.get_mut(headers[i]).unwrap();
*map.entry(val).or_insert(0) += 1;
}
}
for h in &headers {
println!("{} counts:", h);
for (k, v) in &counts[h] {
println!(" {}: {}", k, v);
}
}
}