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:

5087 questions
3827
votes
33 answers

Understanding slice notation

I need a good explanation (references are a plus) on Python's slice notation. To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it.
Simon
  • 70,546
  • 25
  • 83
  • 117
2200
votes
26 answers

JavaScript chop/slice/trim off last character in string

I have a string, 12345.00, and I would like it to return 12345.0. I have looked at trim, but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions?
Phill Pafford
  • 77,927
  • 86
  • 256
  • 378
926
votes
8 answers

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods? String.prototype.slice String.prototype.substring
tmim
  • 9,711
  • 4
  • 16
  • 12
678
votes
23 answers

Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

In order to duplicate an array in JavaScript: which of the following is faster to use? ###Slice method var dup_array = original_array.slice(); ###For loop for(var i = 0, len = original_array.length; i < len; ++i) dup_array[i] =…
Marco Demaio
  • 30,990
  • 33
  • 122
  • 155
624
votes
8 answers

Is there a foreach loop in Go?

Is there a foreach construct in the Go language? Can I iterate over a slice or array using a for?
tatsuhirosatou
  • 22,689
  • 14
  • 37
  • 38
588
votes
7 answers

Concatenate two slices in Go

I'm trying to combine the slice [1, 2] and the slice [3, 4]. How can I do this in Go? I tried: append([]int{1,2}, []int{3,4}) but got: cannot use []int literal (type []int) as type int in append However, the documentation seems to indicate this is…
Kevin Burke
  • 49,451
  • 66
  • 163
  • 280
525
votes
26 answers

Remove last item from array

I have the following array. var arr = [1,0,2]; I would like to remove the last element i.e. 2. I used arr.slice(-1); but it doesn't remove the value.
Prithviraj Mitra
  • 7,940
  • 9
  • 44
  • 80
341
votes
5 answers

How to get last items of a list in Python?

I need the last 9 numbers of a list and I'm sure there is a way to do it with slicing, but I can't seem to get it. I can get the first 9 like this: num_list[0:9]
Nope
  • 29,782
  • 41
  • 91
  • 115
310
votes
10 answers

What is :: (double colon) in Python when subscripting sequences?

I know that I can use something like string[3:4] to get a substring in Python, but what does the 3 mean in somesequence[::3]?
Aillyn
  • 21,054
  • 22
  • 55
  • 82
292
votes
10 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,423
  • 4
  • 14
  • 19
279
votes
17 answers

Contains method for a slice

Is there anything similar to a slice.contains(object) method in Go without having to do a search through each element in a slice?
vosmith
  • 3,280
  • 2
  • 16
  • 26
273
votes
4 answers

Correct way to initialize empty slice

To declare an empty slice, with a non-fixed size, is it better to do: mySlice1 := make([]int, 0) or: mySlice2 := []int{} Just wondering which one is the correct way.
eouti
  • 3,907
  • 3
  • 31
  • 40
229
votes
8 answers

ValueError: setting an array element with a sequence

This Python code: import numpy as p def firstfunction(): UnFilteredDuringExSummaryOfMeansArray = [] MeanOutputHeader=['TestID','ConditionName','FilterType','RRMean','HRMean', …
MedicalMath
  • 2,317
  • 2
  • 13
  • 5
214
votes
8 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,030
  • 8
  • 27
  • 23
213
votes
3 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
  • 12,926
  • 16
  • 67
  • 86
1
2 3
99 100