如何在golang中实现python`zip`功能?

有时,使用zipPython 中的内置函数将两个列表组合成一个元组是很方便的。如何在 golang 中类似地做到这一点?


MYYA
浏览 307回答 2
2回答

MMMHUHU

你可以做这样的事情这样,你给元组类型的名称:package mainimport "fmt"type intTuple struct {&nbsp; &nbsp; a, b int}func zip(a, b []int) ([]intTuple, error) {&nbsp; &nbsp; if len(a) != len(b) {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("zip: arguments must be of same length")&nbsp; &nbsp; }&nbsp; &nbsp; r := make([]intTuple, len(a), len(a))&nbsp; &nbsp; for i, e := range a {&nbsp; &nbsp; &nbsp; &nbsp; r[i] = intTuple{e, b[i]}&nbsp; &nbsp; }&nbsp; &nbsp; return r, nil}func main() {&nbsp; &nbsp; a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}&nbsp; &nbsp; b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}&nbsp; &nbsp; fmt.Println(zip(a, b))}或者为元组使用未命名的类型,如下所示:package mainimport "fmt"func zip(a, b []int) ([][3]int, error) {&nbsp; &nbsp; if len(a) != len(b) {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("zip: arguments must be of same length")&nbsp; &nbsp; }&nbsp; &nbsp; r := make([][4]int, len(a), len(a))&nbsp; &nbsp; for i, e := range a {&nbsp; &nbsp; &nbsp; &nbsp; r[i] = [2]int{e, b[i]}&nbsp; &nbsp; }&nbsp; &nbsp; return r, nil}func main() {&nbsp; &nbsp; a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}&nbsp; &nbsp; b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}&nbsp; &nbsp; fmt.Println(zip(a, b))}最后,这是一种软通用的方法:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")func zip(a, b, c interface{}) error {&nbsp; &nbsp; ta, tb, tc := reflect.TypeOf(a), reflect.TypeOf(b), reflect.TypeOf(c)&nbsp; &nbsp; if ta.Kind() != reflect.Slice || tb.Kind() != reflect.Slice || ta != tb {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: first two arguments must be slices of the same type")&nbsp; &nbsp; }&nbsp; &nbsp; if tc.Kind() != reflect.Ptr {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: third argument must be pointer to slice")&nbsp; &nbsp; }&nbsp; &nbsp; for tc.Kind() == reflect.Ptr {&nbsp; &nbsp; &nbsp; &nbsp; tc = tc.Elem()&nbsp; &nbsp; }&nbsp; &nbsp; if tc.Kind() != reflect.Slice {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: third argument must be pointer to slice")&nbsp; &nbsp; }&nbsp; &nbsp; eta, _, etc := ta.Elem(), tb.Elem(), tc.Elem()&nbsp; &nbsp; if etc.Kind() != reflect.Array || etc.Len() != 2 {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: third argument's elements must be an array of length 2")&nbsp; &nbsp; }&nbsp; &nbsp; if etc.Elem() != eta {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: third argument's elements must be an array of elements of the same type that the first two arguments are slices of")&nbsp; &nbsp; }&nbsp; &nbsp; va, vb, vc := reflect.ValueOf(a), reflect.ValueOf(b), reflect.ValueOf(c)&nbsp; &nbsp; for vc.Kind() == reflect.Ptr {&nbsp; &nbsp; &nbsp; &nbsp; vc = vc.Elem()&nbsp; &nbsp; }&nbsp; &nbsp; if va.Len() != vb.Len() {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("zip: first two arguments must have same length")&nbsp; &nbsp; }&nbsp; &nbsp; for i := 0; i < va.Len(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; ea, eb := va.Index(i), vb.Index(i)&nbsp; &nbsp; &nbsp; &nbsp; tt := reflect.New(etc).Elem()&nbsp; &nbsp; &nbsp; &nbsp; tt.Index(0).Set(ea)&nbsp; &nbsp; &nbsp; &nbsp; tt.Index(1).Set(eb)&nbsp; &nbsp; &nbsp; &nbsp; vc.Set(reflect.Append(vc, tt))&nbsp; &nbsp; }&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}&nbsp; &nbsp; b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}&nbsp; &nbsp; c := [][2]int{}&nbsp; &nbsp; e := zip(a, b, &c)&nbsp; &nbsp; if e != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(e)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(c)}

隔江千里

对于zip一些切片[]int列表,package mainimport "fmt"func zip(lists ...[]int) func() []int {&nbsp; &nbsp; zip := make([]int, len(lists))&nbsp; &nbsp; i := 0&nbsp; &nbsp; return func() []int {&nbsp; &nbsp; &nbsp; &nbsp; for j := range lists {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i >= len(lists[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zip[j] = lists[j][i]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; return zip&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; a := []int{1, 2, 3}&nbsp; &nbsp; b := []int{4, 5, 6}&nbsp; &nbsp; c := []int{7, 8, 9, 0}&nbsp; &nbsp; iter := zip(a, b, c)&nbsp; &nbsp; for tuple := iter(); tuple != nil; tuple = iter() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("tuple:", tuple)&nbsp; &nbsp; }}输出:元组:[1 4 7]元组:[2 5 8]元组:[3 6 9]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go