如何从`other_names`中删除与`names`中相等的元素?

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

How to remove elements from other_names that are equal to elements in names?

问题

  1. 比较切片 'names' 和切片 'other_names';
  2. 如果切片 'names' 的切片(names[1])与切片 'other_names' 的切片(other_name[0][1])具有相同的元素,我们应该删除该切片。

所以,我需要比较 'names' 和 'other_names',并删除切片中具有相同元素的切片。

简单来说,如何删除 'other_name[0][1]',应该是:

var other_names = [][][]string{
    {
        {"Sony", "Bond"},
        {"Piter", "Nina"},
    },
}

以下代码可以工作,但不正确:

func Duplicate() {

    var names = [][]string{
        {"Malder", "Carl"},
        {"Adam", "Kent"},
    }

    var other_names = [][][]string{
        {
            {"Sony", "Bond"},
            {"Adam", "Kent"},
            {"Piter", "Nina"},
        },
    }

    // s := append(s, copies)

    var origl [][]string
    for i := 0; i < len(names); i++ {
        // fmt.Println(names[i][1])
        for v := 0; v < len(copies); v++ {
            for d := 0; d < len(copies[v]); d++ {
                if names[i][1] == copies[v][d][1] {
                    // fmt.Println("点击复制", d, copies[v][d])
                    origl = copies[v][:d]
                    // fmt.Println(origl)
                }
            }
        }
    }
    fmt.Println(origl)
}
英文:
  1. Compare slice 'names' with slice 'other_names';
  2. If slice of slice 'names'(names[1]) have same items that slice of slice 'other_names'(other_name[0][1]), we should delete that slice.

So, I need to compare 'names' and 'other_names' and delete slice of the slice if it have same elements.

Simple. How to delete 'other_name[0][1]', it should be:

var other_names = [][][]string{
		{
			{&quot;Sony&quot;, &quot;Bond&quot;},
			{&quot;Piter&quot;, &quot;Nina&quot;},
		},
	}

The following code works, but not correctly =>

func Duplicate() {

	var names = [][]string{
		{&quot;Malder&quot;, &quot;Carl&quot;},
		{&quot;Adam&quot;, &quot;Kent&quot;},
	}

	var other_names = [][][]string{
		{
			{&quot;Sony&quot;, &quot;Bond&quot;},
			{&quot;Adam&quot;, &quot;Kent&quot;},
			{&quot;Piter&quot;, &quot;Nina&quot;},
		},
	}

	// s := append(s, copies)

	var origl [][]string
	for i := 0; i &lt; len(names); i++ {
		// fmt.Println(names[i][1])
		for v := 0; v &lt; len(copies); v++ {
			for d := 0; d &lt; len(copies[v]); d++ {
				if names[i][1] == copies[v][d][1] {
					// fmt.Println(&quot;点击复制&quot;, d, copies[v][d])
					origl = copies[v][:d]
					// fmt.Println(origl)
				}
			}
		}
	}
	fmt.Println(origl)
}

答案1

得分: 3

将'other_name[0][1]'删除视为一个过滤问题,而不是删除问题。构建一个要保留内容的切片。

编写一个函数来确定一个[]string是否包含在一个[][]string中。

func contains(needle []string, haystack [][]string) bool {
hloop:
	for _, h := range haystack {
		if len(needle) != len(h) {
			continue hloop
		}
		for i := range needle {
			if needle[i] != h[i] {
				continue hloop
			}
		}
		return true
	}
	return false
}

只有在原始切片中的元素不包含在names中时,才将元素从原始切片复制到结果切片。

result := other_names[0][:0]
for _, other_name := range other_names[0] {
	if !contains(other_name, names) {
		result = append(result, other_name)
	}
}
other_names[0] = result
英文:

> How to delete 'other_name[0][1]

Think of this as a filtering problem, not a deletion problem. Construct a slice of what you want to keep.

Write a function to determine whether a []string is contained in a [][]string.

func contains(needle []string, haystack [][]string) bool {
hloop:
	for _, h := range haystack {
		if len(needle) != len(h) {
			continue hloop
		}
		for i := range needle {
			if needle[i] != h[i] {
				continue hloop
			}
		}
		return true
	}
	return false
}

Copy elements from the original slice to the result slice only if the element in the original slice is not contained in names.

result := other_names[0][:0]
for _, other_name := range other_names[0] {
	if !contains(other_name, names) {
		result = append(result, other_name)
	}
}
other_names[0] = result

huangapple
  • 本文由 发表于 2022年11月4日 23:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/74319385.html
匿名

发表评论

匿名网友

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

确定