出现频率最高的N个单词,频率最高的前2个,频率最高的前3个
map的value是无法循环遍历比较的,判断某个key是否在map中,就直接map[那个key],即可判断,其实不需要判断那个key是否在,只需要遇到就m[key]++,因为key在不断的变化
中间map的value要比较找出前n个最大的,因为sort.Slice传入的是切片,所以我们构造一个结构体,去sort这个结构体的count字段就可以了
最后再循环 输出N个出现频率最大的,找个map来存
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
//有一个文件,文件中有很多逗号分隔单词,需要你写一个程序找出文件中出现频率最高的N个单词
func main() {
file, err := ioutil.ReadFile("D:/一体化代码/awesomeProject/test03/aa.txt")
if err != nil {
fmt.Println("read file error")
}
s := string(file)
N := 2
max := FindMax(s, N)
fmt.Println(max)
}
func FindMax(s string, N int) []string {
st := strings.Split(s, ",")
mapCountWord := make(map[string]int)
for _, word := range st {
mapCountWord[word]++
}
//比较
type WordCount struct {
Word string
Count int
}
counts := make([]WordCount, 0, len(mapCountWord))
for word, count := range mapCountWord {
counts = append(counts, WordCount{
word,
count,
})
}
sort.Slice(counts, func(i, j int) bool {
return counts[i].Count > counts[j].Count
})
result := make([]string, 0, N)
for i := 0; i < N && i < len(counts); i++ {
result = append(result, counts[i].Word)
}
return result
}
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
// 这个问题感觉不能使用count作为key去排列,key是唯一的,但是count的值可能会有多个,无法通过count去找到key
func main() {
//用逗号分割
file, err := ioutil.ReadFile("aaa.txt")
if err != nil {
fmt.Println("error:", err)
}
s := string(file)
words := SortWords(s, 3)
fmt.Println(words)
}
func SortWords(s string, N int) []string {
myString := strings.Split(s, ",")
m := make(map[string]int)
/*for i, s2 := range myString {
fmt.Printf("i is %v, s2 is %v \n", i, s2) //索引从0开始
}*/
for _, value := range myString {
m[value]++
}
fmt.Println(m)
words := make([]string, 0, len(m))
for word, _ := range m {
words = append(words, word)
}
sort.Slice(words, func(i, j int) bool {
return m[words[i]] > m[words[j]] //这里根据count的统计次数,已经把次数多的值重新排列在words里面
})
fmt.Println(m)
fmt.Println("排序之后的words: ", words)
//map[aa:8 bb:1 dd:3 ff:4 h:6]
result := make([]string, 0, N)
for i := 0; i < N && i < len(words); i++ {
result = append(result, words[i])
}
return result
}
// the telephone alphabet:
package main
import (
"fmt"
"sort"
)
var (
barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
"delta": 87, "echo": 56, "foxtrot": 12,
"golf": 34, "hotel": 16, "indio": 87,
"juliet": 65, "kili": 43, "lima": 98}
)
func main() {
//将上面的切片排序
fmt.Println("未排序之前")
for k, value := range barVal {
fmt.Printf("key: %v , value: %v \n", k, value)
}
//排序
newSliceKey := make([]string, len(barVal))
i := 0
for k, _ := range barVal {
newSliceKey[i] = k
i++
}
sort.Slice(newSliceKey, func(i, j int) bool {
return newSliceKey[i] <= newSliceKey[j] //从小到大
//return newSliceKey[i] >= newSliceKey[j] //从大到小
})
fmt.Println("排序好的:")
for _, v := range newSliceKey {
fmt.Printf("key: %v , value: %v \n", v, barVal[v])
}
}