英文:
How to remove elements from other_names that are equal to elements in names?
问题
- 比较切片 'names' 和切片 'other_names';
- 如果切片 '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)
}
英文:
- Compare slice 'names' with slice 'other_names';
- 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{
{
{"Sony", "Bond"},
{"Piter", "Nina"},
},
}
The following code works, but not correctly =>
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
得分: 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论