Questions tagged [slice]

A slice is a representation of a part of a sequence, usually defined by a reference to the underlying sequence, an index giving the starting position, a length or end position, and optionally a "stride" or "step" value. Please use the tags "object-slicing" for the slicing problem in C++ and "program-slicing" for the analysis technique.

A slice is a representation of a part of a sequence (including, but not limited to, lists, arrays and strings), usually defined by a reference to the underlying sequence, an index giving the starting position, a length or end position, and optionally a "stride" or "step" value.

Further reading:

6208 questions
2
votes
1 answer

golang sort part of a slice using sort.Slice

It's easy to sort a slice using sort.Slice in Golang, for example func sortAll(nums []int) { sort.Slice(nums, func(i, j int) bool { if nums[i] < nums[j] { return true } else { return false } …
陈建谟
  • 23
  • 4
2
votes
2 answers

find index of biggest values in 2d-Numpy array only in selected region

I have a 2 dimensional Numpy array that contains values. I need to find the index of biggest 100 values in a given slice of the array. The problem is, that the index of the values, must be relative to the whole image, not relative to the slice. So,…
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
2
votes
1 answer

How to get empty list [] instead of null for json.Marshal of []byte?

It easy to get an empty list when working with string by using []string{}: import ( "encoding/json" "fmt" ) func main() { slice1 := []string{} // non-nil but zero-length json1, _ := json.Marshal(slice1) fmt.Printf("%s\n", json1)…
Alireza
  • 6,497
  • 13
  • 59
  • 132
2
votes
1 answer

How is this code generating memory aligned slices?

I'm trying to do direct i/o on linux, so I need to create memory aligned buffers. I copied some code to do it, but I don't understand how it works: package main import ( "fmt" "golang.org/x/sys/unix" "unsafe" …
Mascarpone
  • 2,516
  • 4
  • 25
  • 46
2
votes
1 answer

Why is the .Bytes() value of a 0 big.Int an empty slice?

Why does calling .Bytes() on a zero value big.Int return a slice of length 0? // uint64 var x uint64 xb := make([]byte, 8) binary.BigEndian.PutUint64(xb, x) // [0 0 0 0 0 0 0 0] fmt.Println(xb) // uint8 var y uint8 …
Ramit Mittal
  • 483
  • 3
  • 12
2
votes
1 answer

Table not displaying data when changing page in Angular

I'm trying to make a table displaying data get from an API. I manage to get the 1st page but when changing page, the data is not displayed despite I can see my data from console.log(). Here's the code to call the serice ngOnInit(): void { …
Long Cam
  • 31
  • 4
2
votes
2 answers

How to store data from a HTTP Get request in a slice of structs

Problem I'm new to Go and I'm trying to store json data in a struct from the Gov.uk public holidays API, so I can use this later on in my frontend. If I run var sb = string(body) fmt.Println(sb) I can see the data that's being returned in my…
2
votes
0 answers

Value error for shape size of array with slicing during a loop in python

I am facing an error indicating that there are different array sizes when trying to insert values ​​into a predefined 0 values array using the slice method and for loop. In the three examples array [start:stop], stop-start=5. But a problem occurs…
bennu777
  • 21
  • 2
2
votes
1 answer

Iterating slice struct within struct using reflection

I'm trying to achieve the following: Use-case: I have three structures, I need to compare 2 of those against one. (in the example described as: a & b need to be compared against full) Reflection is used to loop over every field, retrieve the name…
Yadiiiig
  • 45
  • 2
  • 5
2
votes
3 answers

How to retrieve a nested array of objects in mongodb with golang?

I am using Golang/Fiber + Mongo driver. I have simple struct for blog post: type Post struct { ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"` Title *string `json:"title" bson:"title"` Slug …
Andrew
  • 701
  • 1
  • 8
  • 19
2
votes
1 answer

Is it correct to use slice as *[]Item, because Slice is by default pointer

What is the right way to use slice in Go. As per Go documentation slice is by default pointer, so is creating slice as *[]Item is the right way?. Since slice are by default pointer isn't this way of creating the slice making it pointer to a…
Madhusudhan
  • 103
  • 1
  • 1
  • 9
2
votes
4 answers

Is there a R function equivalent to the subset operator `[ ]`, in order to slice by index?

I know that [] is a function itself, but is there a function that does the following ? vect = c(1, 5, 4) # Slicing by row index with [] vect[2] # [1] 5 # Does this kind of function exist ? slicing_func(vect, 2) # [1] 5 # And for dataframes ?
Julien
  • 1,613
  • 1
  • 10
  • 26
2
votes
1 answer

Assign values from slice of unknown length to struct in Go?

I find the case below from some useful packages in github and it looks so ugly and stupid. I think the better design of code can avoid this, but if we meet this, can it be written more concisely? Is there a better way to do that instead of…
sasakiyori
  • 33
  • 5
2
votes
0 answers

Why slice [::-1] is the same as [-1::-1]?

I just wonder why slice [::-1] is the same as [-1::-1]? Syntax for reference: [start:stop:step] It's interesting because almost every Internet article about Python slices states that start defaults to 0. But it seems start defaults to -1 when a step…
filler36
  • 456
  • 7
  • 13
2
votes
2 answers

Why is slice faster than view() when constructing a multidimensional array from a vector?

Consider the following Vector: numbers = Int32[1,2,3,4,5,6,7,8,9,10] If I want to create a 2x5 matrix with the result: 1 2 3 4 5 6 7 8 9 10 I can't use reshape(numbers,2,5) or else I'll get: 1 3 5 7 9 2 4 6 8 10 Using slice or view(), you can…
user19242326