如何在Golang中找到列表对象中的重叠值?

huangapple go评论90阅读模式
英文:

How to find overlapping values in list of objects in golang?

问题

package main

import (
	"fmt"
)

type Column struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

func main() {

	a := []*Column{
		{Name: "a", Type: "int"},
		{Name: "b", Type: "int"},
	}
	b := []*Column{
		{Name: "a", Type: "int"},
		{Name: "c", Type: "string"},
	}
	c := []*Column{
		{Name: "a", Type: "string"},
		{Name: "d", Type: "int"},
	}
}

// 需要找出在比较两个对象列表时,是否存在具有不同类型的重叠名称,如果没有则返回false。有没有关于优化逻辑的建议?

func check(obj1, obj2 []*Column) bool {
	for _, col1 := range obj1 {
		for _, col2 := range obj2 {
			if col1.Name == col2.Name && col1.Type != col2.Type {
				return true
			}
		}
	}
	return false
}

以上是给定的代码,其中定义了一个Column结构体,包含NameType两个字段。在main函数中,创建了三个对象列表abc

check函数用于比较两个对象列表obj1obj2,并判断是否存在具有不同类型的重叠名称。函数通过嵌套的循环遍历两个列表中的每个对象,如果找到具有相同名称但类型不同的对象,则返回true,否则返回false

希望以上信息对你有帮助!如果有任何其他问题,请随时提问。

英文:
package main

import (
	"fmt"
)

type Column struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

func main() {

	a := []*Column{
		{Name: "a", Type: "int"},
		{Name: "b", Type: "int"},
	}
	b := []*Column{
		{Name: "a", Type: "int"},
		{Name: "c", Type: "string"},
	}
    c := []*Column{
    	{Name: "a", Type: "string"},
    	{Name: "d", Type: "int"},
    	}
}

Need to find if there is any overlapping Name with different Type when comparing 2 list of objects, if not return false. Any suggestions for optimized logic?

 func check(obj1,obj2){
 // when comparing a and b it would return false as both Name="a" has identical Type="int"

// when comparing b and c it would return true as both Name="a" have different Types
}

答案1

得分: 0

你可以使用map来存储和比较键:

package main

import (
	"fmt"
)

type Column struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

func check(c1, c2 []*Column) bool {
	m := make(map[string]string)
	for _, col := range c1 {
		m[col.Name] = col.Type
	}
	for _, col := range c2 {
		if t, found := m[col.Name]; found && t != col.Type {
			return true
		}
	}
	return false
}

func main() {

	a := []*Column{
		{Name: "a", Type: "int"},
		{Name: "b", Type: "int"},
	}
	b := []*Column{
		{Name: "a", Type: "int"},
		{Name: "c", Type: "string"},
	}
	c := []*Column{
		{Name: "a", Type: "string"},
		{Name: "d", Type: "int"},
	}

	fmt.Println(check(a, b)) //false
	fmt.Println(check(a, c)) //true
	fmt.Println(check(b, c)) //true
}

Go playground上测试它。

英文:

You can use a map to store and compare the keys:

package main

import (
	"fmt"
)

type Column struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

func check(c1, c2 []*Column) bool {
	m := make(map[string]string)
	for _, col := range c1 {
		m[col.Name] = col.Type
	}
	for _, col := range c2 {
		if t, found := m[col.Name]; found && t != col.Type {
			 return true
		}
	}
	return false
}

func main() {

	a := []*Column{
		{Name: "a", Type: "int"},
		{Name: "b", Type: "int"},
	}
	b := []*Column{
		{Name: "a", Type: "int"},
		{Name: "c", Type: "string"},
	}
	c := []*Column{
		{Name: "a", Type: "string"},
		{Name: "d", Type: "int"},
	}

	fmt.Println(check(a, b)) //false
	fmt.Println(check(a, c)) //true
	fmt.Println(check(b, c)) //true
}

Test it on Go playground

huangapple
  • 本文由 发表于 2022年6月8日 22:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/72547871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定