用go实现python中的collections中的Counter功能

python中collections包中的Counter功能很强大,用过的人都知道,下面用go实现一个简单版本的Counter的most_common功能

  import "sort"

type Data struct {
    Data map[string]int
}

type Counter struct {
    Key   string
    Value int
}

type counterList []Counter

func (c counterList) Len() int           { return len(c) }
func (c counterList) Less(i, j int) bool { return c[i].Value < c[j].Value }
func (c counterList) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }

func (c Data) Count(dataList []string) *Data {

    staticsCount := make(map[string]int)
    //
    //wordsLength := strings.Fields(string(file))

    for _, word := range dataList {
        if _, ok := staticsCount[word]; ok {
            staticsCount[word] = staticsCount[word] + 1
        } else {
            staticsCount[word] = 1
        }
    }
    c.Data = staticsCount
    return &c
}

func (c *Data) MostCommon(n int, reserved bool) []Counter {
    l := len(c.Data)

    list := make(counterList, l)
    idx := 0
    for k, v := range c.Data {
        list[idx] = Counter{
            Key:   k,
            Value: v,
        }
        idx += 1
    }
    if reserved {
        sort.Sort(list)
    } else {
        sort.Sort(sort.Reverse(list))
    }
    if n == 0 {
        return list
    }
    if n < l {
        l = n
    }
    return list[:l]
}


/**
  test函数
*/
func TestCatWcL(t *testing.T) {
    //cat test.txt
    /**
    1
    2
    3
    4
    4
    5
    5
    5
    5
    6
    都会被这个分割
     */
    file, _ := ioutil.ReadFile("test.txt")
    counter := Data{}
    wordsLength := strings.Fields(string(file))
    count := counter.Count(wordsLength).MostCommon(2, false)
    for k, v := range count {
        fmt.Println(k, v)
    }
}

你可能感兴趣的:(用go实现python中的collections中的Counter功能)