[英]How to prepend int to slice
I have a slice which I created using我有一个我使用创建的切片
var x []int;
for i := 2; i < 10; i += 2 {
x = append(x, i);
}
I want to prepend an integer to this slice, something like我想在这个切片前加上一个整数,比如
x = append(2, x)
but obviously it won't work since append needs a slice as the first argument.但显然它不会工作,因为 append 需要一个切片作为第一个参数。
I have tried this but it only works for strings and it's not working in my case.我试过这个,但它只适用于字符串,在我的情况下不起作用。
Use a slice composite literal : []int{1}
, For example:使用切片复合文字: []int{1}
,例如:
package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append([]int{1}, x...)
fmt.Println(x)
}
Playground: https://play.golang.org/p/Yc87gO7gJlD游乐场:https: //play.golang.org/p/Yc87gO7gJlD
Output:输出:
[2 4 6 8]
[1 2 4 6 8]
However, this more efficient version may make fewer allocations, An allocation is only necessary when there is no spare slice capacity.然而,这个更高效的版本可能会进行更少的分配,只有在没有空闲切片容量时才需要分配。
package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append(x, 0)
copy(x[1:], x)
x[0] = 1
fmt.Println(x)
}
Playground: https://play.golang.org/p/fswXul_YfvD游乐场:https: //play.golang.org/p/fswXul_YfvD
Output:输出:
[2 4 6 8]
[1 2 4 6 8]
Good code must be readable.好的代码必须是可读的。 In Go, we often hide implementaion details inside a function.在 Go 中,我们经常将实现细节隐藏在函数中。 Go compilers are optimizing compilers, small, simple functions (like prependInt
) are inlined. Go 编译器正在优化编译器,内联小而简单的函数(如prependInt
)。
package main
import (
"fmt"
)
func prependInt(x []int, y int) []int {
x = append(x, 0)
copy(x[1:], x)
x[0] = y
return x
}
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(len(x), cap(x), x)
x = prependInt(x, 1)
fmt.Println(len(x), cap(x), x)
}
Playground: https://play.golang.org/p/wl6gvoXraKH游乐场:https: //play.golang.org/p/wl6gvoXraKH
Output:输出:
4 4 [2 4 6 8]
5 8 [1 2 4 6 8]
See Go SliceTricks .请参阅Go SliceTricks 。
The current version is go1.14.11当前版本是go1.14.11
Prepend without a for loop:没有 for 循环的前置:
package main
import "fmt"
func main () {
data := []int{1, 2, 3, 4}
fmt.Println(data)
data = append([]int{99}, data...)
fmt.Println(data)
}
Example taken form: https://codingair.wordpress.com/2014/07/18/go-appendprepend-item-into-slice/示例形式: https ://codingair.wordpress.com/2014/07/18/go-appendprepend-item-into-slice/
Works with integers: https://play.golang.org/p/gaLhB5_d1Iu使用整数:https: //play.golang.org/p/gaLhB5_d1Iu
Another answer uses append
and copy
, but you can do it with just append
, in one or two lines:另一个答案使用append
和copy
,但您可以在一两行中仅使用append
来完成:
package main
import "fmt"
func main() {
{
a := []int{20, 30}
a = append(append(a, 10), a...)[len(a):]
fmt.Println(a)
}
{
a := []int{20, 30}
a = append(make([]int, 1), a...)
a[0] = 10
fmt.Println(a)
}
{ // if starting with empty slice, use: a := make([]int, 0, 1)
a := []int{20, 30}
a = append(a[:1], a...)
a[0] = 10
fmt.Println(a)
}
}
Or as another option, you can use linked list:或者作为另一种选择,您可以使用链表:
package main
import (
"container/list"
"fmt"
)
func main() {
a := list.New()
a.PushBack(20)
a.PushBack(30)
a.PushFront(10)
for n := a.Front(); n != nil; n = n.Next() {
fmt.Println(n.Value)
}
}
https://golang.org/pkg/container/list https://golang.org/pkg/container/list
I know the Question is only for int
but thought I'd add this to the discussion:我知道这个问题只针对int
但我想我会把这个添加到讨论中:
https://play.golang.com/p/PNax2a1rL3q https://play.golang.com/p/PNax2a1rL3q
import (
"golang.org/x/exp/constraints"
)
type Number interface {
constraints.Signed | constraints.Unsigned | constraints.Float
}
func prepend[T Number](list []T, elems ...T) []T {
results := make([]T, len(list)+len(elems))
results = append(results, 0)
copy(results[len(elems):], list)
copy(results[:len(elems)], elems)
return results
}
References:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.