亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

內部循環定義父鏈接

內部循環定義父鏈接

Go
蝴蝶刀刀 2023-06-19 14:04:53
我想循環我的表的名稱以添加由符號“ _”定義的關聯。如果表a_b然后存在a則, 。最后,我不必打印名稱中包含“”的表ba = [b]b = [a]_結構體// Table with Fields and Assoctype Table struct {    Name       string    Assoc      []Assoc}// Assoc is a name of associated Tabletype Assoc struct {  Name string}tables := []string{    "a",    "b",    "c",    "d",    "f",    "a_b",    "a_c",    "a_d_f",    "c_d",      }var tbls []Tablefor _, t := range tables {    if strings.Contains(t, "_") {        // Split to find "_" like assoc := strings.Split(t, "_") ?        // append in struct "Table{Name:a, Assoc:  [b,c,d,f]}"        // append in struct "Table{Name:b, Assoc:  [a]}"        // append in struct "Table{Name:c, Assoc:  [a,d]}"        // append in struct "Table{Name:d, Assoc:  [a,c,f]}"        // append in struct "Table{Name:f, Assoc:  [a,d]}"          } else {        n := Table{            Name: t,        }        tbls = append(tbls, n)    }}像這樣返回fmt.Println(tbls):[{a [b,c,d,f]} {b [a]} {c [a,d]} {d [a,c,f]} {f [a,d]}]去游樂場
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

使用地圖完成上述操作 https://play.golang.org/p/8C5M0L-es6o

package main


import (

    "fmt"

    "strings"

)


// Table with Fields and Assoc

type Table struct {

    Name  string

    Assoc map[string]int

}


// Assoc is a name of associated Table

// type Assoc struct {

//  Name string

// }


func main() {

    tables := []string{

        "a",

        "b",

        "c",

        "d",

        "f",

        "a_b",

        "a_c",

        "a_d_f",

        "c_d",

    }


    var tbls = make(map[string]map[string]int)


    for _, t := range tables {

        if strings.Contains(t, "_") {

            splitAssocs := strings.Split(t, "_")            

            for i:=0;i<=len(splitAssocs)-2;i++ {

                for j:=(i+1);j<=len(splitAssocs)-1;j++{

                    _, ok := tbls[splitAssocs[i]]

                    if !ok{

                        tbls[splitAssocs[i]] = make(map[string]int)

                    }

                     _, ok = tbls[splitAssocs[j]]

                    if !ok{

                        tbls[splitAssocs[j]] = make(map[string]int)

                    }

                    tbls[splitAssocs[i]][splitAssocs[j]] = 1

                    tbls[splitAssocs[j]][splitAssocs[i]] = 1

                }


            }


        } else {

            _, ok := tbls[t]            

            if !ok{

                tbls[t] = make(map[string]int)

            }

        }


    }


    fmt.Println(tbls)

}


查看完整回答
反對 回復 2023-06-19
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號