比较两个切片是否缺少元素

我有两片:a []string, b []string. b包含所有相同的元素加上a一个额外的元素。索引:值彼此不匹配,但值匹配,否则我可以执行类似 和strings.Join()的操作strings.Replace()。这就是我正在尝试的。


package main


import (

    "fmt"

    "github.com/google/go-cmp/cmp"

)


func compare(start_keys []string, end_keys []string) string {

    match := make([]string, len(start_keys))

    q := 0

    for i := 0; i < len(end_keys); i++ {

        for z := 0; z < len(start_keys); z++ {

            if end_keys[i] == start_keys[z] {

                match[q] = start_keys[z]

                q++

            }

        }

    } // now matches known


    for i := 0; i < len(end_keys); i++ {

        n := end_keys[i]   // current element to compare to

        s := nip(match, n) // compare result

        if s == true {     // if no matches, print

            a := end_keys[i]

            fmt.Println("Not present: ", a)

        }

        i++

    }

    fmt.Println("List=", match)

    return "error"

}


func nip(matches []string, element string) bool {

    zyx := 0

    for n := 0; n < len(matches); n++ {

        if matches[n] == element {

            n++

            zyx++

        } else if matches[n] != element {

            n++

        }

    }

    if zyx == 0 {

        return true

    }

    return false

}


func main() {

    start_keys := []string{"a", "b", "c", "d", "e", "f", "g"}

    end_keys := []string{"a", "1sdsdfsdfsdsdf", "c", "d", "e", "f", "g", "b"}


    x := compare(start_keys, end_keys)


    x = x

    foo := cmp.Diff(start_keys, end_keys)

    fmt.Println(foo)

}

matches[]符合我的预期,但nip()在某些匹配项上返回 true,而不是针对唯一值1sdsdfsdfsdsdf


a

a :::: a

c

a :::: c

d :::: c

f :::: c

b :::: c

Not present:  c

e

a :::: e

d :::: e

f :::: e

b :::: e

Not present:  e

g

a :::: g

d :::: g

f :::: g

b :::: g


jeck猫
浏览 120回答 2
2回答

HUWWW

比较两个切片是否缺少元素我有两片:a []string, b []string. b包含所有相同的元素加上a一个额外的元素。例如,package mainimport (&nbsp; &nbsp; "fmt")func missing(a, b []string) string {&nbsp; &nbsp; ma := make(map[string]bool, len(a))&nbsp; &nbsp; for _, ka := range a {&nbsp; &nbsp; &nbsp; &nbsp; ma[ka] = true&nbsp; &nbsp; }&nbsp; &nbsp; for _, kb := range b {&nbsp; &nbsp; &nbsp; &nbsp; if !ma[kb] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return kb&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return ""}func main() {&nbsp; &nbsp; a := []string{"a", "b", "c", "d", "e", "f", "g"}&nbsp; &nbsp; b := []string{"a", "1sdsdfsdfsdsdf", "c", "d", "e", "f", "g", "b"}&nbsp; &nbsp; fmt.Println(missing(a, b))}输出:1sdsdfsdfsdsdf

浮云间

/*An example of how to find the difference between two slices.This example uses empty struct (0 bytes) for map values.*/package mainimport (&nbsp; &nbsp; "fmt")// empty struct (0 bytes)type void struct{}// missing compares two slices and returns slice of differencesfunc missing(a, b []string) []string {&nbsp; &nbsp; // create map with length of the 'a' slice&nbsp; &nbsp; ma := make(map[string]void, len(a))&nbsp; &nbsp; diffs := []string{}&nbsp; &nbsp; // Convert first slice to map with empty struct (0 bytes)&nbsp; &nbsp; for _, ka := range a {&nbsp; &nbsp; &nbsp; &nbsp; ma[ka] = void{}&nbsp; &nbsp; }&nbsp; &nbsp; // find missing values in a&nbsp; &nbsp; for _, kb := range b {&nbsp; &nbsp; &nbsp; &nbsp; if _, ok := ma[kb]; !ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diffs = append(diffs, kb)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return diffs}func main() {&nbsp; &nbsp; a := []string{"a", "b", "c", "d", "e", "f", "g"}&nbsp; &nbsp; b := []string{"a", "c", "d", "e", "f", "g", "b"}&nbsp; &nbsp; c := []string{"a", "b", "x", "y", "z"}&nbsp; &nbsp; fmt.Println("a and b diffs", missing(a, b))&nbsp; &nbsp; fmt.Println("a and c diffs", missing(a, c))}输出a and b diffs []a and c diffs [x y z]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go