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

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

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

问题

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Column struct {
  6. Name string `json:"name"`
  7. Type string `json:"type"`
  8. }
  9. func main() {
  10. a := []*Column{
  11. {Name: "a", Type: "int"},
  12. {Name: "b", Type: "int"},
  13. }
  14. b := []*Column{
  15. {Name: "a", Type: "int"},
  16. {Name: "c", Type: "string"},
  17. }
  18. c := []*Column{
  19. {Name: "a", Type: "string"},
  20. {Name: "d", Type: "int"},
  21. }
  22. }
  23. // 需要找出在比较两个对象列表时,是否存在具有不同类型的重叠名称,如果没有则返回false。有没有关于优化逻辑的建议?
  24. func check(obj1, obj2 []*Column) bool {
  25. for _, col1 := range obj1 {
  26. for _, col2 := range obj2 {
  27. if col1.Name == col2.Name && col1.Type != col2.Type {
  28. return true
  29. }
  30. }
  31. }
  32. return false
  33. }

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

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

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

英文:
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Column struct {
  6. Name string `json:"name"`
  7. Type string `json:"type"`
  8. }
  9. func main() {
  10. a := []*Column{
  11. {Name: "a", Type: "int"},
  12. {Name: "b", Type: "int"},
  13. }
  14. b := []*Column{
  15. {Name: "a", Type: "int"},
  16. {Name: "c", Type: "string"},
  17. }
  18. c := []*Column{
  19. {Name: "a", Type: "string"},
  20. {Name: "d", Type: "int"},
  21. }
  22. }

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?

  1. func check(obj1,obj2){
  2. // when comparing a and b it would return false as both Name="a" has identical Type="int"
  3. // when comparing b and c it would return true as both Name="a" have different Types
  4. }

答案1

得分: 0

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

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Column struct {
  6. Name string `json:"name"`
  7. Type string `json:"type"`
  8. }
  9. func check(c1, c2 []*Column) bool {
  10. m := make(map[string]string)
  11. for _, col := range c1 {
  12. m[col.Name] = col.Type
  13. }
  14. for _, col := range c2 {
  15. if t, found := m[col.Name]; found && t != col.Type {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func main() {
  22. a := []*Column{
  23. {Name: "a", Type: "int"},
  24. {Name: "b", Type: "int"},
  25. }
  26. b := []*Column{
  27. {Name: "a", Type: "int"},
  28. {Name: "c", Type: "string"},
  29. }
  30. c := []*Column{
  31. {Name: "a", Type: "string"},
  32. {Name: "d", Type: "int"},
  33. }
  34. fmt.Println(check(a, b)) //false
  35. fmt.Println(check(a, c)) //true
  36. fmt.Println(check(b, c)) //true
  37. }

Go playground上测试它。

英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Column struct {
  6. Name string `json:"name"`
  7. Type string `json:"type"`
  8. }
  9. func check(c1, c2 []*Column) bool {
  10. m := make(map[string]string)
  11. for _, col := range c1 {
  12. m[col.Name] = col.Type
  13. }
  14. for _, col := range c2 {
  15. if t, found := m[col.Name]; found && t != col.Type {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func main() {
  22. a := []*Column{
  23. {Name: "a", Type: "int"},
  24. {Name: "b", Type: "int"},
  25. }
  26. b := []*Column{
  27. {Name: "a", Type: "int"},
  28. {Name: "c", Type: "string"},
  29. }
  30. c := []*Column{
  31. {Name: "a", Type: "string"},
  32. {Name: "d", Type: "int"},
  33. }
  34. fmt.Println(check(a, b)) //false
  35. fmt.Println(check(a, c)) //true
  36. fmt.Println(check(b, c)) //true
  37. }

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:

确定