在go中循环切片/数组的有效方法

时间:2017-12-07 03:01:55

标签: arrays performance loops go slice

基本上我有这个:

package main

import "fmt"

type Struct1 struct {
    id   int
    name string
}

type Struct2 struct {
    id       int
    lastname string
}

type Struct3 struct {
    id   int
    real bool
}

func main() {
    var (
     s1 []Struct1
     s2 []Struct2
     s3 []Struct3
    )
    s1 = append(s1, Struct1{id: 1, name: "Eliot"}, Struct1{id: 2, name: "Tyrell"}, Struct1{id: 3, name: "Mr Robot"})
    s2 = append(s2, Struct2{id: 1, lastname: "Anderson"}, Struct2{id: 2, lastname: "Wellick"})
    s3 = append(s3, Struct3{id: 1, real: true}, Struct3{id: 2, real: true}, Struct3{id: 3, real: false})
}

我想展示这样的东西:

  • 艾略特安德森真实(真实)
  • Tyrell Wellick真实(真实)

但我不想在s2内循环s1然后在s3内循环

示例:

for i := 0; i < len(s1); i++ {
        for j := 0; j < len(s2); j++ {
            if s1[i].id == s2[j].id {
                for k := 0; k < len(s3); k++ {
                    if s2[j].id == s3[k].id {
                        // some code ...
                    }
                }
            }
        }
    }

那么,有什么其他方法可以做到这一点?

3 个答案:

答案 0 :(得分:0)

使用地图按人名索引数据。

private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
    MakeTextBoxEditable(itxt_CommonTitle);
}

private void itxt_CommonTitle_Leave(object sender, EventArgs e)
{
    MakeTextBoxReadOnly(itxt_CommonTitle);
}

private void Form1_Click(object sender, EventArgs e)
{
    MakeTextBoxReadOnly(itxt_CommonTitle);
}


private Color origTextBoxBackColor = SystemColors.Control;

private void MakeTextBoxEditable(TextBox textBox)
{
    origTextBoxBackColor = textBox.BackColor;
    textBox.ReadOnly = false;
    textBox.BackColor = Color.White;
    textBox.Focus();
}

private void MakeTextBoxReadOnly(TextBox textBox)
{
    textBox.ReadOnly = true;
    textBox.BackColor = origTextBoxBackColor;
}

然后查找并将数据放入地图。

答案 1 :(得分:0)

正确的方法是将它们放入哈希(在Golang中称为map)。通过这种方式,您可以获得性能,而您只能使用一个循环迭代id。

以下是您的示例数据示例:

   function rot13(str) { // LBH QVQ VG!
        console.log (str);
        var newStr = str;
        var ascii = "";
        for (var i = 0; i < newStr.length; i++){
            var letter = newStr.charAt(i);
            //console.log (letter);
            var code = letter.charCodeAt();
            //console.log (code);


            if (code > 77){
                ascii = code - 13;
                //console.log (ascii);
            }
            else if (code === 32 ){
                ascii = code;
                //console.log (ascii);
            }
            else{
                ascii = code + 13;
                //console.log (ascii);
            }
            var sipher = "";
            sipher = String.fromCharCode(ascii);
            newStr += sipher;

        }
        console.log (sipher);

            return newStr;
    }

输出:

package main

import (
    "fmt"
)

type Struct1 struct {
    id   int
    name string
}

type Struct2 struct {
    id       int
    lastname string
}

type Struct3 struct {
    id   int
    real bool
}

func main() {
    //var (
    //s1 []Struct1
    //      s2 []Struct2
    //  s3 []Struct3
    //  )
    s1Hash := make(map[int]Struct1)
    s2Hash := make(map[int]Struct2)
    s3Hash := make(map[int]Struct3)

    s11 := Struct1{id: 1, name: "Eliot"}
    s12 := Struct1{id: 2, name: "Tyrell"}
    s13 := Struct1{id: 3, name: "Mr Robot"}
    s1Hash[s11.id] = s11
    s1Hash[s12.id] = s12
    s1Hash[s13.id] = s13

    s21 := Struct2{id: 1, lastname: "Anderson"}
    s22 := Struct2{id: 2, lastname: "Wellick"}
    s2Hash[s21.id] = s21
    s2Hash[s22.id] = s22

    s31 := Struct3{id: 1, real: true}
    s32 := Struct3{id: 2, real: true}
    s33 := Struct3{id: 3, real: false}
    s3Hash[s31.id] = s31
    s3Hash[s32.id] = s32
    s3Hash[s33.id] = s33

    //s1 = append(s1, Struct1{id: 1, name: "Eliot"}, Struct1{id: 2, name: "Tyrell"}, Struct1{id: 3, name: "Mr Robot"})
    //s2 = append(s2, Struct2{id: 1, lastname: "Anderson"}, Struct2{id: 2, lastname: "Wellick"})
    //s3 = append(s3, Struct3{id: 1, real: true}, Struct3{id: 2, real: true}, Struct3{id: 3, real: false})

    //i to loop over possible id range
    for i := 1; i <= len(s1Hash); i++ {
        fmt.Println("i is ", i)
        if _, ok := s1Hash[i]; ok {
            fmt.Printf("Name: %s ", s1Hash[i].name)
        }

        if _, ok := s2Hash[i]; ok {
            fmt.Printf(" Lastname: %s ", s2Hash[i].lastname)
        }

        if _, ok := s3Hash[i]; ok {
            fmt.Printf(" Real: %t\n", s3Hash[i].real)
        }
        //fmt.Printf("%s %s real:%t\n", s1Hash[i].name, s2[i].lastname, s3[i].real)
    }

}

在操场上检查this。希望这有帮助!

P.S。 :最后,如果您删除某些ID的所有结构条目并添加更新的ID,您可以考虑将有效的ID添加到地图i is 1 Name: Eliot Lastname: Anderson Real: true i is 2 Name: Tyrell Lastname: Wellick Real: true i is 3 Name: Mr Robot Real: false map[int]bool )并使用mymap[id] = true而不是上面的range迭代地图。

答案 2 :(得分:-1)

事实上,要加入所有第一个元素,然后是所有第二个元素,你不需要在一个循环中进行循环:

准备每个切片作为地图:

m1 := make(map[int]string)
for i:=0; i < len(s1); i ++ {
    m1[s1[i].id] = s1[i].Name
}

另外2个切片也是如此。 最后迭代一次:

for i:=0; i < len(s1); i ++ {
    fmt.Println(m1[i], m2[i], m3[i])
}

此解决方案假设所有切片都有相应的项目。如果没有,你应该决定如何处理没有片段的元素:ignore,用一些占位符替换blank,打破整个周期等。例如,你决定firstName是必需的(我们将按它迭代),{ {1}}是可选的,如果缺席,应该用secondName替换,并且对于整个周期来说,real是绝对必须的 - 如果它不存在,我们会打破进一步的工作并返回空切片:

?