Go 實現的 python collections

clearT發表於2019-08-19

Go 實現的 python collections

接觸 Go 的時間不算長,之前一直是寫 pythonPHP 這類動態語言的,python 確實強大,靈活性高,但同樣會給閱讀程式碼帶來困難, 學習了Go 之後發現程式碼很簡潔,想通過寫一些 Go 程式碼來熟悉 Gopython collections 庫裡面的資料結構是我認為經常用到的,很方便,因此用 Go 實現了一下,實現了 chainmap, counter, deque, queue, orderedmapset 結構。

  • chain map
    cm := NewChainMap() // empty chain map
    child := map[string]interface{}{
        "country": "China",
        "age":     21,
    }
    cm.NewChild(child) // add a new map element
    other := map[string]interface{}{
                    "name":   "gensword",
                    "gender": "man",
                    "age":    22,
                }
    cm = NewChainMap(child, other) // get a chain map for child and other, return map[string]interface{}{"country": "China", "age": 21, "gender": "man"}
    keys := cm.Keys() // return keys for chain map
    values := cm.Values() // return values for chain map
    parentsChainMap := cm.Parents() // return a new chain map except the first map for cm chain map
    parentsChainMap.Maps() // return a map slice
    cm.Map["name"] // get gensword
  • counter
    counter := NewCounter("a", "b", "c", "d", "b") // init a counter with some elements
    counter.Add(1) // add one
    counter.Add(1) // now twice 1
    top2 := counter.MostCommon(2) // get a slice like PairList{Pair{1, 2}, Pair{"b", 2}} that indicate the top 2 elements and their counts
    counter.Elements() //return all elements
    counter.Del(1)
    len := counter.Len()
  • deque
    dq := NewDeque()
    dq.AppendLeft(1) // push left
    dq.Append("gensword") // push right
    dq.Pop() // pop right
    dq.PopLeft() // pop left
    dq.Extend() // extend a *list.List right
    dq.ExtendLeft() // extend a *list.List left
    dq.Rotate(1) // right step 1
    dq.Rotate(-1) // left step 1
    dq.Index(1, 0, dq.Size()) // return the first position index of element and true which value is 1 between 0 and len(dq), if not found, return zero and false
    dq.Remove(1) // delete the first element satisfied with 1 and return remove nums(1) and true, if not found, return zero and false
    dq.Clear() // clear all elements
  • queue
    q := Newqueue()
    q.Push(1)
    q.push("hello")
    q.Pop()
    q.Size() // return 1
    q.Pop()
    q.Isempty() // return true
  • orderedMap

    om := NewOrderedMap()
    om.Set("name", "gensword", true) // set name, the last bool argument is a flag if the name should replace to the last position and update the key when key name already in om, if false, just update the name value
    om.Set("age", 21)
    v, ok := om.Get("age") // get the value of age, if not found, reutn nil and false
    for item := range om.Iter() {
    
    } // the first item will be map[interface{}]interface{}{"name": "gensword"}, and the second will be map[interface{}]interface{}{"age": 21}
    om.Del("name") // delete the key name, if not exists, return false
  • set
    s := NewSet(1, "hello") // init a set with two elements
    s.Add('I') // add one
    s.Exists(1) // true
    s.Exists(2) // false
    other := NewSet("you", "hello")
    intersect := s.Intersect(other) // return a new set include "hello"
    union := s.Union(other) // return a new set include 1, "hello", "you", 'I'
    diff := s.Diff(other) // return a new set include 1 , 'I', and "you"
    s.Elemetns // return []interface{}{1, "hello", 'I'}

github here https://github.com/gensword/collections

Go 新手,希望各位多多指正,給予好的意見。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章