如何在切片中跳过第二个元素?

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

How to skip the second element in a slice?

问题

在这段代码中,我有一个包含四个字符串的切片,但我想跳过第二个字符串。如何实现呢?

期望的结果

Title
Profession
Gender

代码

package main

import "fmt"

func main() {
    data := [...]string{"Title", "Description", "Profession", "Gender"}
    for _, val := range data[1:] {
        fmt.Println(val)
    }
}
英文:

In this code, I have four strings in a slice but I want to skip the second one. How to do it?

Required Result

Title
Profession
Gender

Code

package main

import "fmt"

func main() {
	data := [...]string{"Title", "Description", "Profession", "Gender"}
	for _, val := range data[1:] {
		fmt.Println(val)
	}
}

答案1

得分: 1

遍历整个切片并跳过第二个元素。

range 的第一个返回值是当前元素的索引(从0开始)。你可以使用它来决定是否要跳过。continue 关键字会立即跳到内层循环的下一个元素,而不执行当前步骤的循环体的其余部分。

package main

import "fmt"

func main() {
	data := [...]string{"Title", "Description", "Profession", "Gender"}
	for i, val := range data {
		if i == 1 {
			continue
		}
		fmt.Println(val)
	}
}
英文:

Iterate over the entire slice and skip the second element.

First return value of range is index (starting from 0) of current element. You can use that to decide if you want to skip or not. continue keyword will immediately iterate to the next element of the inner-most loop without executing the rest of the loop body for current step.

package main

import "fmt"

func main() {
	data := [...]string{"Title", "Description", "Profession", "Gender"}
	for i, val := range data {
		if i == 1 {
			continue
		}
		fmt.Println(val)
	}
}

答案2

得分: 1

如果你的切片始终相同,你可以这样做:

package main

import "fmt"

func main() {
    data := [...]string{"Title", "Description", "Profession", "Gender"}
    for i, val := range data {
        if i == 1 {
            continue
        }
        fmt.Println(val)
    }
}

但是,如果你想跳过"Description",而不管切片的顺序如何,你可以这样做:

func main() {
    data := [...]string{"Title", "Gender", "Profession", "Description"}
    for _, val := range data {
        if val == "Description" {
            continue
        }
        fmt.Println(val)
    }
}

在第一个示例中,你将根据数组的索引进行过滤,而在第二个示例中,你将根据当前数组索引的实际值进行过滤。

英文:

If your slice is always the same than you can do it like this: <br>

package main

import &quot;fmt&quot;

func main() {
	data := [...]string{&quot;Title&quot;, &quot;Description&quot;, &quot;Profession&quot;, &quot;Gender&quot;}
	for i, val := range data {
		if i == 1 {
			continue
		}
		fmt.Println(val)

	}
}

<br>
But, if you want to skip the Description no matter the slice order than you can do it like this: <br>
<br>

func main() {
	data := [...]string{&quot;Title&quot;, &quot;Gender&quot;, &quot;Profession&quot;, &quot;Description&quot;}
	for _, val := range data {
		if val == &quot;Description&quot; {
			continue
		}
		fmt.Println(val)

	}
}

<br>
In the first example you will filter by the index of the array, in the second you will filter by the actual value of the current array index.

答案3

得分: 1

你可以使用append来实现跳过操作,代码如下:

package main

import "fmt"

func main() {
	idxToSkip := 1
	data := [...]string{"Title", "Description", "Profession", "Gender"}
	for _, val := range append(data[:idxToSkip], data[idxToSkip+1:]...) {
		fmt.Println(val)
	}
}

这段代码的作用是跳过指定索引位置的元素,并打印出剩余的元素。

英文:

You can use append to skip like this

package main

import &quot;fmt&quot;

func main() {
	idxToSkip := 1
	data := [...]string{&quot;Title&quot;, &quot;Description&quot;, &quot;Profession&quot;, &quot;Gender&quot;}
	for _, val := range append(data[:idxToSkip], data[idxToSkip+1:]...) {
		fmt.Println(val)
	}
}

huangapple
  • 本文由 发表于 2022年9月18日 21:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/73763072.html
匿名

发表评论

匿名网友

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

确定