slice Questions

4

Solved

I am working to implement a code which was written in MATLAB into C++. In MATLAB you can slice an Array with another array, like A(B), which results in a new array of the elements of A at the index...
Gessner asked 6/5, 2021 at 22:4

1

Given a []int, e.g: is := []int{2, 4, 1, 3} Can sort via: sort.Sort(), e.g: sort.Sort(sort.IntSlice(is)) slices.Sort(), e,g: slices.Sort(is) I know slices.Sort() is experience, from "gol...
Corbett asked 2/4, 2023 at 7:38

10

Solved

I have a slice with ~2.1 million log strings in it, and I would like to create a slice of slices with the strings being as evenly distributed as possible. Here is what I have so far: // logs is a...
Faultfinder asked 3/2, 2016 at 14:23

3

Solved

I have a list of elements and I want to remove one of them, by value. In Python this would be l = ["apples", "oranges", "melon"] l.remove("melon") print(l) # ["apples", "orange"] What is the equ...
Shoal asked 26/6, 2015 at 18:55

4

Consider the following piece of code: def func1(a): a[:] = [x**2 for x in a] a = range(10) print a #prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] func1(a[:5]) print a #also prints [0, 1, 2, 3, 4, 5, 6, ...
Reset asked 20/2, 2017 at 13:48

5

Solved

Given a map, and a list of keys val abc = mapOf(1 to "a", 2 to "b", 3 to "c") val keys = listOf(1, 2) How do I a get a map containing only the key-value pairs specifi...
Dextrin asked 28/1, 2021 at 14:37

4

Solved

Situation: I've a slice of values and need to pick up a randomly chosen value from it. Then I want to concatenate it with a fixed string. This is my code so far: func main() { //create the reason...
Control asked 30/11, 2015 at 8:55

3

Solved

I'm recently exploring Go and how goroutines work confuse me. I tried to port code I had written before into Go using goroutines but got a fatal error: all goroutines are asleep - deadlock! error....
Architecture asked 2/9, 2017 at 5:41

7

Solved

It seems I am getting lost in something potentially silly. I have an n-dimensional numpy array, and I want to multiply it with a vector (1d array) along some dimension (which can change!). As an e...
Darnelldarner asked 4/5, 2015 at 13:48

7

Solved

In Python, I have the following: i = series.index(s) # standard Python list.index() function tmp = series.pop(i) blah = f(tmp) series.append(tmp) In converting this to Go, I am looking for a sim...
Kukri asked 27/9, 2018 at 23:46

9

Solved

If I have a list: to_modify = [5,4,3,2,1,0] And then declare two other lists: indexes = [0,1,3,5] replacements = [0,0,0,0] How can I take to_modify's elements as index to indexes, then set c...
Madelynmademoiselle asked 29/8, 2011 at 14:4

5

Solved

a=[1,2,3] b=[4,5,6] c=[] d=[] Whats the difference between these two statements? c[:]=a d=b[:] But both gives the same result. c is [1,2,3] and d is [4,5,6] And is there any difference funct...
Josiah asked 2/7, 2012 at 16:41

2

Solved

Is it possible to initialize an slice with all 1's like in python? PYTHON: onesArray = np.ones(5) onesList = [1]*5 GOLANG onesSlice := make([]int, 5) for i:= 0; i < len(onesSlice); i++{ on...
Performative asked 11/10, 2016 at 19:13

4

Solved

This question is about the speed of accessing elements of arrays and slices, not about the efficiency of passing them to functions as arguments. I would expect arrays to be faster than slices in m...
Enameling asked 29/5, 2015 at 8:49

6

Solved

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...
Heretofore asked 12/12, 2018 at 6:43

4

Solved

What is the best way to check whether a certain value is in a string slice? I would use a Set in other languages, but Go doesn't have one. My best try is this so far: package main import "fmt" ...
Bildungsroman asked 22/11, 2012 at 21:18

10

Solved

Powershell's array notation has rather bizarre, albeit documented, behavior for slicing the end of arrays. This section from the official documentation sums up the bizarreness rather well: Negat...
Vespertine asked 11/2, 2015 at 17:7

12

Solved

I have an array of strings, and I'd like to exclude values that start in foo_ OR are longer than 7 characters. I can loop through each element, run the if statement, and add it to a slice along th...
Amphibian asked 1/6, 2016 at 8:5

18

Solved

Let's say I have a list of student cities and the size of it could be 100 or 1000, and I want to filter out all duplicates cities. I want a generic solution that I can use to remove all duplicate s...
Cheadle asked 15/3, 2021 at 18:52

25

fmt.Println("Enter position to delete::") fmt.Scanln(&pos) new_arr := make([]int, (len(arr) - 1)) k := 0 for i := 0; i < (len(arr) - 1); { if i != pos { new_arr[i] = arr[k] k++ i++ } e...
Jehoash asked 19/5, 2016 at 21:15

3

When taking a substring of a string in Go, no new memory is allocated. Instead, the underlying representation of the substring contains a Data pointer that is an offset of the original string's Dat...
Feinberg asked 4/6, 2013 at 4:39

4

Solved

I have a slice that contains work to be done, and a slice that will contain the results when everything is done. The following is a sketch of my general process: var results = make([]Result, len(j...
Republicanism asked 17/4, 2018 at 13:23

3

Solved

I have 2 slices, s1 := []int{1, 2, 3, 4, 5} s2 := []int{3, 4, 5, 6, 7} I want the resultant s3 = []int{1, 2, 3, 4, 5, 3, 4, 5, 6, 7} I am doing something like: for _, x := range s1 { s2 =...
Homology asked 19/9, 2017 at 16:47

9

Solved

I have a slice of structs. type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a response body into the above slice if err ...
Decade asked 29/7, 2016 at 8:46

2

Solved

I catch Consider preallocating [to] (prealloc) this problen in golangci-lint my code is: var to []string for _, t := range s.To { to = append(to, t.String()) } Do you have an idea to resolve t...
Chretien asked 14/1, 2020 at 13:25

© 2022 - 2024 — McMap. All rights reserved.