Nicht wirklich sicher, ob dies Ihre Frage beantwortet, aber dies ist eine einfache rekursive Implementierung, um die Ausgabe unten zu finden.
package main
import "fmt"
func main() {
values := [][]int{}
// These are the first two rows.
row1 := []int{1, 2, 3}
row2 := []int{4, 5, 6}
row3 := []int{7, 8, 9}
// Append each row to the two-dimensional slice.
values = append(values, row1)
values = append(values, row2)
values = append(values, row3)
fmt.Println(getPermutation(values))
}
func getPermutation(vids [][]int) [][]int {
toRet := [][]int{}
if len(vids) == 0 {
return toRet
}
if len(vids) == 1 {
for _, vid := range vids[0] {
toRet = append(toRet, []int{vid})
}
return toRet
}
t := getPermutation(vids[1:])
for _, vid := range vids[0] {
for _, perm := range t {
toRetAdd := append([]int{vid}, perm...)
toRet = append(toRet, toRetAdd)
}
}
return toRet
}
https://play.golang.org/p/f8wktrxkU0
Ausgabe des obigen Auszug:
[[1 4 7] [1 4 8] [1 4 9] [1 5 7] [1 5 8] [1 5 9] [1 6 7] [1 6 8] [1 6 9] [2 4 7] [2 4 8] [2 4 9] [2 5 7] [2 5 8] [2 5 9] [2 6 7] [2 6 8] [2 6 9] [3 4 7] [3 4 8] [3 4 9] [3 5 7] [3 5 8] [3 5 9] [3 6 7] [3 6 8] [3 6 9]]
'itertools.product' gibt Ihnen die cartesianischen Produkt einiger Sätze. Es gibt dir keine Permutationen. Obwohl Sie die kartesischen Produkte verwenden könnten, um die Permutationen herauszufinden, wäre das fürchterlich ineffizient. http://docs.python.org/2/library/itertools.html#itertools.product – scvalex
Ich bin ein Idiot. Das habe ich immer durcheinander gebracht, und ich habe ein Paket für kartesische Produkte gefunden. Danke – Colum
@Colum Sie haben einen Fehler gemacht; das macht dich nicht zum Idioten. –