fork download
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6.  
  7. func main() {
  8. data := `outlook temperature humidity windy play
  9. sunny hot high FALSE no
  10. sunny hot high TRUE no
  11. overcast hot high FALSE yes
  12. rainy mild high FALSE yes
  13. rainy cool normal FALSE yes
  14. rainy cool normal TRUE no
  15. overcast cool normal TRUE yes
  16. sunny mild high FALSE no
  17. sunny cool normal FALSE yes
  18. rainy mild normal FALSE yes
  19. sunny mild normal TRUE yes
  20. overcast mild high TRUE yes
  21. overcast hot normal FALSE yes
  22. rainy mild high TRUE no`
  23.  
  24. lines := strings.Split(data, "\n")
  25. headers := strings.Split(lines[0], "\t")
  26. counts := make(map[string]map[string]int)
  27. for _, h := range headers {
  28. counts[h] = make(map[string]int)
  29. }
  30. for _, line := range lines[1:] {
  31. fields := strings.Split(line, "\t")
  32. for i, val := range fields {
  33. counts[headers[i]][val]++
  34. }
  35. }
  36. for _, h := range headers {
  37. fmt.Println(h, "counts:")
  38. for val, count := range counts[h] {
  39. fmt.Printf(" %s: %d\n", val, count)
  40. }
  41. }
  42. }
  43.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
outlook counts:
  rainy: 5
  sunny: 5
  overcast: 4
temperature counts:
  mild: 6
  cool: 4
  hot: 4
humidity counts:
  normal: 7
  high: 7
windy counts:
  FALSE: 8
  TRUE: 6
play counts:
  no: 5
  yes: 9