gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/listx/constraints/group.go (about) 1 package constraints 2 3 import "math" 4 5 // GroupByCount 按指定数量分组 6 func (list *List[V]) GroupByCount(count int) (r []*List[V]) { 7 list.rlock() 8 groupLen := int(math.Ceil(float64(list.Length()) / 8)) 9 for i := 0; i < groupLen; i++ { 10 a := &List[V]{} 11 start := i * count 12 end := int(math.Min(float64(start+count), float64(list.Length()))) 13 for j := start; j < end; j++ { 14 a.push(list.At(j)) 15 } 16 17 r = append(r, a) 18 } 19 list.runlock() 20 return 21 } 22 23 func (list *List[V]) GroupByCustomKey(getKey func(item V) string) map[string]*List[V] { 24 list.rlock() 25 r := map[string]*List[V]{} 26 for _, val := range list.Slice() { 27 key := getKey(val) 28 a, has := r[key] 29 if !has { 30 a = &List[V]{} 31 r[key] = a 32 } 33 a.Push(val) 34 } 35 list.runlock() 36 return r 37 }