Go 实现的 python collections

gensword · 2019-08-19 20:02:23 · 2147 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2019-08-19 20:02:23 的主题,其中的信息可能已经有所发展或是发生改变。

接触 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 新手,希望各位多多指正,给予好的意见。


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2147 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传