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...
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...
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...
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...
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, ...
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...
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....
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...
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...
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...
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...
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...
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...
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"
...
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...
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...
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...
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...
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 =...
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 ...
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...
1 Next >
© 2022 - 2024 — McMap. All rights reserved.