golang将切片或数组进行分组
例子一:
golang 数组分割
需求:给定一个数组和一个正整数,要求把数组分割成多个正整数大小的数组,如果不够分,则最后一个数组分到剩余的所有元素。
package main
import (
"fmt"
)
func main() {
//demo01()
//demo02()
demo03()
}
func demo01() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9,10}
fmt.Println(arr) //[1 2 3 4 5 6 7 8 9 10]
res:= ArrayInGroupsOf(arr,2) //[[1 2] [3 4] [5 6] [7 8] [9 10]]
fmt.Println(res)
}
func demo02() {
arr := [][]string{{"1", "2"},{"3", "4"},{"5", "6"},{"7", "8"},{"9","10"}}
fmt.Println(arr) //[[1 2] [3 4] [5 6] [7 8] [9 10]]
res:= ArrayTwoStringGroupsOf(arr,5) //[[[1 2] [3 4]] [[5 6] [7 8]] [[9 10]]]
fmt.Printf("%+v",res)
}
type student struct {
name string
age int
}
func demo03() {
stus := []student{
{name: "小王子", age: 18},
{name: "娜扎", age: 23},
{name: "大王八", age: 9000},
{name: "大王八1", age: 9000},
{name: "大王八2", age: 9000},
{name: "大王八3", age: 9000},
{name: "大王八4", age: 9000},
}
fmt.Printf("%+v\n",stus)
//[{name:小王子 age:18} {name:娜扎 age:23} {name:大王八 age:9000} {name:大王八1 age:9000} {name:大王八2 age:9000} {name:大王八3 age:9000} {name:大王八4 age:9000}]
res:= ArrayStruGroupsOf(stus,3) //[[[1 2] [3 4]] [[5 6] [7 8]] [[9 10]]]
fmt.Printf("%+v\n",res)
//[[{name:小王子 age:18} {name:娜扎 age:23} {name:大王八 age:9000}] [{name:大王八1 age:9000} {name:大王八2 age:9000} {name:大王八3 age:9000}] [{name:大王八4 age:9000}]]
}
/*
示例3:
数组:[{name:小王子 age:18} {name:娜扎 age:23} {name:大王八 age:9000} {name:大王八1 age:9000} {name:大王八2 age:9000} {name:大王八3 age:9000} {name:大王八4 age:9000}],正整数:2
期望结果: [[{name:小王子 age:18} {name:娜扎 age:23} {name:大王八 age:9000}] [{name:大王八1 age:9000} {name:大王八2 age:9000} {name:大王八3 age:9000}] [{name:大王八4 age:9000}]]
调用: res:= arrayStruGroupsOf(arr,2)
*/
func ArrayStruGroupsOf(stu []student, num int64) [][]student {
max := int64(len(stu))
//判断数组大小是否小于等于指定分割大小的值,是则把原数组放入二维数组返回
if max <= num {
return [][]student{stu}
}
//获取应该数组分割为多少份
var quantity int64
if max%num == 0 {
quantity = max / num
} else {
quantity = (max / num) + 1
}
//声明分割好的二维数组
var segments = make([][]student, 0)
//声明分割数组的截止下标
var start, end, i int64
for i = 1; i <= quantity; i++ {
end = i * num
if i != quantity {
segments = append(segments, stu[start:end])
} else {
segments = append(segments, stu[start:])
}
start = i * num
}
return segments
}
/*
示例1:
数组:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],正整数:2
期望结果: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
调用: res:= arrayInGroupsOf(arr,2)
*/
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
}
/*
示例2:
数组:[[1 2] [3 4] [5 6] [7 8] [9 10]],正整数:2
期望结果: [[[1 2] [3 4]] [[5 6] [7 8]] [[9 10]]]
调用: res:= arrayTwoStringGroupsOf(arr,2)
*/
func ArrayTwoStringGroupsOf(arr [][]string, num int64) [][][]string {
max := int64(len(arr))
//判断数组大小是否小于等于指定分割大小的值,是则把原数组放入二维数组返回
if max <= num {
return [][][]string{arr}
}
//获取应该数组分割为多少份
var quantity int64
if max%num == 0 {
quantity = max / num
} else {
quantity = (max / num) + 1
}
//声明分割好的二维数组
var segments = make([][][]string, 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/