A volte, è conveniente combinare due elenchi in una tupla usando la funzione integrata zip
in Python. Come farlo nello stesso modo in Golang?come implementare la funzione `zip` di python in golang?
9
A
risposta
10
Si potrebbe fare qualcosa di simile this, in cui si dà un nome del tipo di parametri:
package main
import "fmt"
type intTuple struct {
a, b int
}
func zip(a, b []int) ([]intTuple, error) {
if len(a) != len(b) {
return nil, fmt.Errorf("zip: arguments must be of same length")
}
r := make([]intTuple, len(a), len(a))
for i, e := range a {
r[i] = intTuple{e, b[i]}
}
return r, nil
}
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
fmt.Println(zip(a, b))
}
Oppure, in alternativa utilizzare un tipo senza nome per la tupla, come this:
package main
import "fmt"
func zip(a, b []int) ([][3]int, error) {
if len(a) != len(b) {
return nil, fmt.Errorf("zip: arguments must be of same length")
}
r := make([][4]int, len(a), len(a))
for i, e := range a {
r[i] = [2]int{e, b[i]}
}
return r, nil
}
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
fmt.Println(zip(a, b))
}
E infine here's un modo soft-generico di farlo:
package main
import (
"fmt"
"reflect"
)
func zip(a, b, c interface{}) error {
ta, tb, tc := reflect.TypeOf(a), reflect.TypeOf(b), reflect.TypeOf(c)
if ta.Kind() != reflect.Slice || tb.Kind() != reflect.Slice || ta != tb {
return fmt.Errorf("zip: first two arguments must be slices of the same type")
}
if tc.Kind() != reflect.Ptr {
return fmt.Errorf("zip: third argument must be pointer to slice")
}
for tc.Kind() == reflect.Ptr {
tc = tc.Elem()
}
if tc.Kind() != reflect.Slice {
return fmt.Errorf("zip: third argument must be pointer to slice")
}
eta, _, etc := ta.Elem(), tb.Elem(), tc.Elem()
if etc.Kind() != reflect.Array || etc.Len() != 2 {
return fmt.Errorf("zip: third argument's elements must be an array of length 2")
}
if etc.Elem() != eta {
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")
}
va, vb, vc := reflect.ValueOf(a), reflect.ValueOf(b), reflect.ValueOf(c)
for vc.Kind() == reflect.Ptr {
vc = vc.Elem()
}
if va.Len() != vb.Len() {
return fmt.Errorf("zip: first two arguments must have same length")
}
for i := 0; i < va.Len(); i++ {
ea, eb := va.Index(i), vb.Index(i)
tt := reflect.New(etc).Elem()
tt.Index(0).Set(ea)
tt.Index(1).Set(eb)
vc.Set(reflect.Append(vc, tt))
}
return nil
}
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
c := [][2]int{}
e := zip(a, b, &c)
if e != nil {
fmt.Println(e)
return
}
fmt.Println(c)
}
4
Per zip
un determinato numero di fetta []int
liste,
package main
import "fmt"
func zip(lists ...[]int) func() []int {
zip := make([]int, len(lists))
i := 0
return func() []int {
for j := range lists {
if i >= len(lists[j]) {
return nil
}
zip[j] = lists[j][i]
}
i++
return zip
}
}
func main() {
a := []int{1, 2, 3}
b := []int{4, 5, 6}
c := []int{7, 8, 9, 0}
iter := zip(a, b, c)
for tuple := iter(); tuple != nil; tuple = iter() {
fmt.Println("tuple:", tuple)
}
}
uscita:
tuple: [1 4 7] tuple: [2 5 8] tuple: [3 6 9]
Problemi correlati
- 1. Zip FileSystem in Golang
- 2. Come implementare la registrazione basata sul livello in golang?
- 3. Come implementare la funzione GROWTH in JavaScript
- 4. funzione Senzacorpo in Golang
- 5. Come implementare zip con foldl (in una lingua ansiosa)
- 6. Come implementare la funzione di "ottimizzazione" di R in C++?
- 7. Come implementare una buona funzione __hash__ in python
- 8. Qualcosa come python timedelta in golang
- 9. Come testare una funzione sleep in golang
- 10. Come implementare la funzione getch() di C in Linux?
- 11. Come posso implementare la funzione di cancellazione in SVG?
- 12. list :: moreutils maglia o la funzione 'zip'
- 13. NSArray. Come posso implementare la funzione Mappa?
- 14. Equivalente di Golang per la comprensione della lista di Python
- 15. File zip valido in python
- 16. Come implementare un timer watchdog in Python?
- 17. Golang come utilizzare la funzione come chiave della mappa
- 18. Come implementare funzionalità come la funzione "Nuova storia" di Facebook?
- 19. Analog of setdefault di Python in golang
- 20. Funzione zip dell'unione canonica esterna
- 21. come implementare la funzione di timeout pubsub di redis?
- 22. Python: come implementare __getattr __()?
- 23. Come implementare la funzione "Ricordami" in un'attività Android?
- 24. Come implementare la funzione media generica in scala?
- 25. Ottieni il nome della funzione utilizzando la riflessione in Golang
- 26. Come implementare la pura funzione virtuale in C++
- 27. Come implementare la funzione make_unique in C++ 11?
- 28. Implementare SMPP in Python
- 29. Come implementare questa funzione in Haskell
- 30. come implementare la funzione di ricerca utilizzando Javascript o jquery