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
319
votes
11 answers

How to take column-slices of dataframe in pandas

I load some machine learning data from a CSV file. The first 2 columns are observations and the remaining columns are features. Currently, I do the following: data = pandas.read_csv('mydata.csv') which gives something like: data =…
cpa
  • 3,742
  • 4
  • 16
  • 22
314
votes
7 answers

Select rows in pandas MultiIndex DataFrame

What are the most common pandas ways to select/filter rows of a dataframe whose index is a MultiIndex? Slicing based on a single value/label Slicing based on multiple labels from one or more levels Filtering on boolean conditions and…
cs95
  • 379,657
  • 97
  • 704
  • 746
304
votes
9 answers

ValueError: setting an array element with a sequence

Why do the following code samples: np.array([[1, 2], [2, 3, 4]]) np.array([1.2, "abc"], dtype=float) ...all give the following error? ValueError: setting an array element with a sequence.
MedicalMath
  • 3,067
  • 2
  • 15
  • 5
266
votes
4 answers

How to slice an array in Bash

Looking the "Array" section in the bash(1) man page, I didn't find a way to slice an array. So I came up with this overly complicated function: #!/bin/bash # @brief: slice a bash array # @arg1: output-name # @arg2: input-name # @args: seq args #…
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
257
votes
4 answers

How to get the last element of a slice?

What is the Go way for extracting the last element of a slice? var slice []int slice = append(slice, 2) slice = append(slice, 7) slice[len(slice)-1:][0] // Retrieves the last element The solution above works, but seems awkward.
Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
248
votes
9 answers

Explanation of [].slice.call in javascript?

I stumbled onto this neat shortcut for converting a DOM NodeList into a regular array, but I must admit, I don't completely understand how it works: [].slice.call(document.querySelectorAll('a'), 0) So it starts with an empty array [], then slice is…
Yansky
  • 4,580
  • 8
  • 29
  • 24
226
votes
3 answers

How to join a slice of strings into a single string?

package main import ( "fmt" "strings" ) func main() { reg := [...]string {"a","b","c"} fmt.Println(strings.Join(reg,",")) } gives me an error of: prog.go:10: cannot use reg (type [3]string) as type []string in argument to strings.Join Is there…
cycle4passion
  • 3,081
  • 3
  • 14
  • 28
207
votes
4 answers

How do you use the ellipsis slicing syntax in Python?

This came up in Hidden features of Python, but I can't see good documentation or examples that explain how the feature works.
miracle2k
  • 29,597
  • 21
  • 65
  • 64
197
votes
4 answers

What is a concise way to create a 2D slice in Go?

I am learning Go by going through A Tour of Go. One of the exercises there asks me to create a 2D slice of dy rows and dx columns containing uint8. My current approach, which works, is this: a:= make([][]uint8, dy) // initialize a slice of dy…
hazrmard
  • 3,397
  • 4
  • 22
  • 36
191
votes
7 answers

Slicing of a NumPy 2d array, or how do I extract an mxm submatrix from an nxn array (n>m)?

I want to slice a NumPy nxn array. I want to extract an arbitrary selection of m rows and columns of that array (i.e. without any pattern in the numbers of rows/columns), making it a new, mxm array. For this example let us say the array is 4x4 and I…
levesque
  • 8,756
  • 10
  • 36
  • 44
188
votes
11 answers

Why can't I duplicate a slice with `copy()`?

I need to make a copy of a slice in Go and reading the docs there is a copy function at my disposal. The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a …
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
184
votes
15 answers

Grab a segment of an array in Java without creating a new array on heap

I'm looking for a method in Java that will return a segment of an array. An example would be to get the byte array containing the 4th and 5th bytes of a byte array. I don't want to have to create a new byte array in the heap memory just to do that. …
jbu
  • 15,831
  • 29
  • 82
  • 105
163
votes
5 answers

How do you clear a slice in Go?

What is the appropriate way to clear a slice in Go? Here's what I've found in the go forums: // test.go package main import ( "fmt" ) func main() { letters := []string{"a", "b", "c", "d"} fmt.Println(cap(letters)) …
Chris Weber
  • 5,555
  • 8
  • 44
  • 52
157
votes
6 answers

How to return a part of an array in Ruby?

With a list in Python I can return a part of it using the following code: foo = [1,2,3,4,5,6] bar = [10,20,30,40,50,60] half = len(foo) / 2 foobar = foo[:half] + bar[half:] Since Ruby does everything in arrays I wonder if there is something similar…
Christian Stade-Schuldt
  • 4,671
  • 7
  • 35
  • 30
154
votes
5 answers

In Go how to get a slice of values from a map?

If I have a map m is there a better way of getting a slice of the values v than this? package main import ( "fmt" ) func main() { m := make(map[int]string) m[1] = "a" m[2] = "b" m[3] = "c" m[4] = "d" // Can this be done…
masebase
  • 4,915
  • 3
  • 24
  • 20