两个切片内容相减的几种方法
文章发布较早,内容可能过时,阅读注意甄别。
问题
已知: var a []string = []string{"11", "33", "22", "44"} var b []string = []string{"11", "22"} var b []string 预期: c = []string{"11", "22"}
1
2
3
4
5
6
7
# 1,运维的思路
# 方法一
package main
import "fmt"
func main() {
var (
weightInstanceList []string = []string{"11", "22", "33", "44"}
RelateInstance []string = []string{"11", "22"}
a []string // 期望:22,33,44
)
fmt.Println(weightInstanceList, RelateInstance)
// 循环老的
for _, instance := range weightInstanceList {
// 传入待删除IP切片 老的IP 进行判断 是否相等 相等表示待删除 反之加入更新jenkinsFile列表
isOk := IsContain(RelateInstance, instance)
if !isOk {
a = append(a, instance)
}
}
fmt.Println("最后结果:", a) // a=["11","11","11"]
}
func IsContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 方法二
package main
import "fmt"
func main() {
var (
weightInstanceList []string = []string{"11", "33", "22", "44"}
RelateInstance []string = []string{"11", "22"}
)
fmt.Println(weightInstanceList[1:])
for _, instance := range weightInstanceList {
for _, item := range RelateInstance {
if instance == item {
weightInstanceList = RemoveParam(weightInstanceList, instance)
}
}
}
fmt.Println("最后结果:", weightInstanceList) // a=["11","11","11"]
}
func RemoveParam(sli []string, n string) []string {
for i := 0; i < len(sli); i++ {
if sli[i] == n {
if i == 0 {
sli = sli[1:]
} else if i == len(sli)-1 {
sli = sli[:i]
} else {
sli = append(sli[:i], sli[i+1:]...)
}
i-- // 如果索引i被去掉后,原来索引i+1的会移动到索引i
}
}
return sli
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 2,开发的思路
# 方法一
package main
import "fmt"
func main() {
var (
weightInstanceList []string = []string{"11", "33", "22", "44"}
RelateInstance []string = []string{"11", "22"}
)
fmt.Println(diff(weightInstanceList, RelateInstance))
}
func diff(a, b []string) []string {
var (
r []string
m = make(map[string]struct{}, len(a))
)
for _, v := range b {
m[v] = struct{}{}
}
for _, v := range a {
if _, ok := m[v]; !ok {
r = append(r, v)
}
}
return r
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 方法二
package main
import "fmt"
func main() {
var (
weightInstanceList []string = []string{"11", "33", "22", "44"}
RelateInstance []string = []string{"11", "22"}
)
fmt.Println(diff(weightInstanceList, RelateInstance))
}
func diff(a, b []string) []string {
var (
r []string
m = make(map[string]struct{}, len(a))
)
for _, v := range a {
m[v] = struct{}{}
}
for _, v := range b {
delete(m, v)
}
for k, _ := range m {
r = append(r, k)
}
return r
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
可以看出,运维处理该问题时思路会困在当前类型中予以实现,不如开发对数据结构以及特性掌握的牢固,运用起来也就不够灵活了。
上次更新: 2024/11/19, 23:11:42