## Why are slice and range upper-bound exclusive?

Disclaimer: I am not asking if the upper-bound stopargument of slice()and range() is exclusive or how to use these functions. Calls to the rangeand slicefunctions, as well as the slice notation [start:stop] all refer to sets of integers. range([start], stop[, step]) slice([start], stop[, step]) In all these, the stop integer is excluded. I am wondering … Read more

## How to check if a slice has a given index in Go?

We can easily do that with maps: item, ok := myMap[“index”] But not with slices: item, ok := mySlice[3] // panic! Surprised this wasn’t asked before. Maybe I’m on the wrong mental model with Go slices? Answer There is no sparse slices in Go, so you could simply check the length: if len(mySlice) > 3 … Read more

## Slicing a vector in C++

Is there an equivalent of list slicing [1:] from Python in C++ with vectors? I simply want to get all but the first element from a vector. Python’s list slicing operator: list1 = [1, 2, 3] list2 = list1[1:] print(list2) # [2, 3] C++ Desired result: std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2; v2 … Read more

## What is the idiomatic way to slice an array relative to both of its ends?

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: Negative numbers count from the end of the array. For example, “-1” refers to the last element of the array. To display the last three elements of the … Read more

## How to return all except last 2 characters of a string?

id = ’01d0′; document.write(‘<br/>’+id.substr(0,-2)); How can I take a string like ’01d0and get the01` (all except the last two chars)? In PHP I would use substr(0,-2) but this doesn’t seem to work in JavaScript. How can I make this work? Answer You are looking for slice() (also see MDC) id.slice(0, -2) AttributionSource : Link , … Read more

## Is it possible to initialize slice with specific values?

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++{ onesSlice[i] = 1 } Is it possible to do better than this? Answer Yes but you have to use a different syntax. oneSlice … Read more

## Use slice notation with collections.deque

How would you extract items 3..6 efficiently, elegantly and pythonically from the following deque without altering it: from collections import deque q = deque(”,maxlen=10) for i in range(10,20): q.append(i) the slice notation doesn’t seem to work with deque… Answer import itertools output = list(itertools.islice(q, 3, 7)) For example: >>> import collections, itertools >>> q = … Read more

## Most idiomatic way to select elements from an array in Golang?

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 the way. But I was curious if there was an idiomatic or more golang-like way of … Read more

## Pick a random value from a Go Slice

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 reasons slice and append reasons to it reasons := make([]string, 0) reasons = append(reasons, “Locked out”, “Pipes … Read more

## How to slice a list from an element n to the end in python?

I’m having some trouble figuring out how to slice python lists, it is illustrated as follows: >>> test = range(10) >>> test [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> test[3:-1] [3, 4, 5, 6, 7, 8] >>> test[3:0] [] >>> test[3:1] [] >>> test [0, 1, 2, 3, 4, 5, 6, … Read more