zoukankan      html  css  js  c++  java
  • golang将切片或数组进行分组

    例子一:
    golang 数组分割
    需求:给定一个数组和一个正整数,要求把数组分割成多个正整数大小的数组,如果不够分,则最后一个数组分到剩余的所有元素。

    示例1:
    
    数组:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],正整数:2
    期望结果: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    
    示例2:
    
    数组:[1, 2, 3, 4, 5, 6, 7, 8, 9],正整数:2
    期望结果: [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
    

    代码

    package main
     
    import (
        "fmt"
    )
    
    func main(){
    
    	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9,10}	//[[1 2] [3 4] [5 6] [7 8] [9 10]]
            fmt.Println(arr) //[1 2 3 4 5 6 7 8 9 10]
    	//arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}	//[[1 2] [3 4] [5 6] [7 8] [9]]
    
    	res:= arrayInGroupsOf(arr,2)
    	fmt.Println(res)
    }
    
    func arrayInGroupsOf(arr []int, num int64) [][]int {
    	max := int64(len(arr))
    	//判断数组大小是否小于等于指定分割大小的值,是则把原数组放入二维数组返回
    	if max <= num {
    		return [][]int{arr}
    	}
    	//获取应该数组分割为多少份
    	var quantity int64
    	if max%num == 0 {
    		quantity = max / num
    	} else {
    		quantity = (max / num) + 1
    	}
    	//声明分割好的二维数组
    	var segments = make([][]int, 0)
    	//声明分割数组的截止下标
    	var start, end, i int64
    	for i = 1; i <= quantity; i++ {
    		end = i*num
    		if i != quantity {
    			segments = append(segments, arr[start:end])
    		} else {
    			segments = append(segments, arr[start:])
    		}
    		start = i*num
    	}
    	return segments
    }
    

    原文链接:https://blog.csdn.net/MCJPAO/article/details/108431657

    例子二

    golang将切片或数组根据某个字段进行分组

    package main
    
    import (
    	"fmt"
    	"sort"
    )
    
    type Person struct {
    	Name string
    	Age int
    }
    
    func main() {
    	p1 := Person{"Tom",20}
    	p2 := Person{"Lily",21}
    	p3 := Person{"Linda",23}
    	p4 := Person{"Jass",25}
    	p5 := Person{"Tonny",20}
    	p6 := Person{"Pite",25}
    	p7 := Person{"Paul",21}
    	p8 := Person{"Kriss",27}
    	p9 := Person{"Jake",23}
    	p10 := Person{"Rose",20}
    
    	personList := []Person{}
    	personList = append(personList,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
    	fmt.Println("分组前的数据:",personList)
    	fmt.Println("分组后的数据:",splitSlice(personList))
    }
    
    //按某个字段排序
    type sortByAge []Person
    
    func (s sortByAge) Len() int           { return len(s) }
    func (s sortByAge) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    func (s sortByAge) Less(i, j int) bool { return s[i].Age < s[j].Age}
    
    
    
    //切片分组
    func splitSlice(list []Person)  [][]Person {
    	sort.Sort(sortByAge(list))
    	returnData := make([][]Person,0)
    	i:= 0
    	var j int
    	for {
    		if i >= len(list) {
    			break
    		}
    		for j = i + 1; j< len(list) && list[i].Age == list [j].Age; j++ {}
    
    		returnData = append(returnData,list[i:j])
    		i = j
    	}
    	return returnData
    }
    
    

    输出:

    分组前的数据: [{Tom 20} {Lily 21} {Linda 23} {Jass 25} {Tonny 20} {Pite 25} {Paul 21} {Kriss 27} {Jake 23} {Rose 20}]
    分组后的数据: [[{Tom 20} {Rose 20} {Tonny 20}] [{Lily 21} {Paul 21}] [{Linda 23} {Jake 23}] [{Pite 25} {Jass 25}] [{Kriss 27}]]
    
    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    react文本溢出hover气泡显示全部文本——JS判断文本溢出
    ecosystem.config
    git删除中间某次提交
    HTML5 新元素
    HTML5存储
    简述LVS(Linux虚拟服务器)
    简述线程池
    网络协议基础
    JVM常用性能检测工具
    线程间的“通信”方法
  • 原文地址:https://www.cnblogs.com/haima/p/15130998.html
Copyright © 2011-2022 走看看