dongzongpeng6474 2019-07-09 13:22
浏览 76
已采纳

删除Gota系列的重复值

I have a simple gota series and I want to remove duplicate from it. how it is possible in go?

func main() {

    new_series := series.New([]string{"b", "a", "c", "a", "d", "b"}, series.String, "COL.1")
    fmt.Println(new_series)
}

[b a c a d b]

expected: [b a c d]

  • 写回答

1条回答 默认 最新

  • douyinbo3361 2019-07-09 14:07
    关注

    To represent a unique set of elements in go, use a map.

    So to create a unique set of strings from a given list, use something like:

    func setFromList(list []string) (set []string) {
        ks := make(map[string]bool) // map to keep track of repeats
    
        for _, e := range list {
            if _, v := ks[e]; !v {
                ks[e] = true
                set = append(set, e)
            }
        }
        return
    }
    

    and to apply this to an existing gota.Series:

    func uniqueGotaSeries(s series.Series) series.Series {
        return series.New(setFromList(s.Records()), s.Type(), s.Name)
    }
    

    Working playground example: https://play.golang.org/p/zqQM-0XxLF5

    Output:

    Orig: [b a c a d b]
    Unique: [b a c d]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥15 Mabatis查询数据