drzil26260 2019-08-11 02:34
浏览 30
已采纳

比较两个缺少元素的切片

I have two slices: a[]string, b[]string. b contains all the same elements as a plus an extra. The indexes:values don't match one another but the values do, otherwise I could do something like strings.Join() and strings.Replace(). Here's what I'm trying.

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[] holds what I expect but nip() is returning true on certain matches and not for the unique value 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
  • 写回答

2条回答 默认 最新

  • dongshao1021 2019-08-11 03:10
    关注

    Comparing two slices for missing element

    I have two slices: a[]string, b[]string. b contains all the same elements as a plus an extra.

    For example,

    package main
    
    import (
        "fmt"
    )
    
    func missing(a, b []string) string {
        ma := make(map[string]bool, len(a))
        for _, ka := range a {
            ma[ka] = true
        }
        for _, kb := range b {
            if !ma[kb] {
                return kb
            }
        }
        return ""
    }
    
    func main() {
        a := []string{"a", "b", "c", "d", "e", "f", "g"}
        b := []string{"a", "1sdsdfsdfsdsdf", "c", "d", "e", "f", "g", "b"}
        fmt.Println(missing(a, b))
    }
    

    Output:

    1sdsdfsdfsdsdf
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等