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
2
votes
1 answer

Function doesn't want to return string slice using function arguments

I tried making a function that returns a string slice. The function takes three arguments: a the start of the slice b the end of slice txt a reference to a string literal fn main() { let text = String::from("My name is Ivan"); fn…
2
votes
1 answer

Custom slice from list of lists

I have this list MAIN = [ ['ABC', '562', '112', '80', '231', '217', '433', '115', '10'], ['ABC', '562', '112', '80', '231', '322', '202', '432', '12'], ['ABC', '562', '112', '80', '231', '677', '133', '255', '64'], …
Ger Cas
  • 2,188
  • 2
  • 18
  • 45
2
votes
1 answer

Is there a consistent way to force errors on incorrect list or vector indexing

My expectation from other programming languages is that (1:4)[3:5] and list(asdf = 4, qwerty = 5)$asdg should both raise exceptions. Instead, the first silently returns c(3, 4, NA), and the second silently returns NULL (as does or list(asdf = 4,…
2
votes
1 answer

In Rust, why doesn't &v[1..] panic when the size of vector v is one?

In the gcd example in Programming Rust, 2nd Edition, why doesn't &numbers[1..] cause an out-of-bounds error in the statement for m in &numbers[1..] when the size of vector numbers is one? Doesn't numbers[1] address the second element which is one…
Derek Mahar
  • 27,608
  • 43
  • 124
  • 174
2
votes
2 answers

Inconsitent behaviour with computation of "greatest product given 'n' adjacent digits" (D language)

I'm working on a solution for Project Euler (problem 8) which involves working out the largest product of thirteen adjacent digits. I wrote a solution (below) but it doesn't seem to be outputting the correct answer when the length of the slice is…
Aleksey
  • 65
  • 4
2
votes
3 answers

Calculate the average of sections of a column with condition met to create new dataframe

I have the below data table A = [2, 3, 1, 2, 4, 1, 5, 3, 1, 7, 5] B = [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0] df = pd.DataFrame({'A':A, 'B':B}) I'd like to calculate the average of column A when consecutive rows see column B equal to 1. All rows where…
2
votes
3 answers

Finding Max Value Below Zero

I'm trying to get the max value from a diff, n[i] - n[i-1], timeseries. The first value is always zero from the slice, here is the code: func MaxBelowZero(n ...float64) float64 { var maxValue float64 if len(n) == 1 { return n[0] } else if len(n)…
JMaia
  • 79
  • 4
2
votes
2 answers

Converting a slice of arrays into an array of slices

I have a slice of fixed size arrays (&[[T; N]]), and I'd like to be able to view this as an array of slices ([&[T]; N]). The i-th element of the output array would be a slice of all of the i-th items from each array in the original slice: fn…
Mark LeMoine
  • 4,478
  • 3
  • 31
  • 52
2
votes
3 answers

Why append slice in Go change the original slice?

I have this code: // The input param is A := []int{3, 4, 5, 3, 7} func someFunc(A []int) int { ... ways := 0 i := 0 for i < len(A) { if i+1 == len(A) || i == len(A) { fmt.Println("break") break …
alramdein
  • 810
  • 2
  • 12
  • 26
2
votes
2 answers

Splitting a boxed slice in two owned halves

My problem I have a boxed slice s: Box<[Something]> that I would like to split into two owned halves, e.g., s1: Box<[Something]> containing the first s.len()/2 elements of s, and s2: Box<[Something]> containing the rest. With Vecs I know something…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
2
votes
4 answers

Get a slice of a sorted list according to a key in Python

Is it possible to slice a sorted list based on some key value (e.g the length of the list items)? If so, how? For example I get a sorted list like this: sorted_list = sorted(some_list, key=len) Now I want to get a slice containing all items with…
Guilty
  • 464
  • 4
  • 13
2
votes
2 answers

Can't change the code to list comprehension

My question is kind of simple yet I don't seem to find the answer. I have the following line in my code that works fine: Uniquesample = np.matrix([[ 1. , 0. ], [ 2. , -106.965], [ 3. , …
2
votes
1 answer

Sns barplot does not sort sliced values

I want to plot from pd df using sns barplot. Everything works fine : code associated : result = df.groupby(['Code departement']).size().sort_values(ascending=False) x=result.index y=result.values plot=sns.barplot(x, y) plot.set(xlabel='Code…
user16049328
2
votes
1 answer

Slice Operators | Catching the 2nd element in every sub list | Python

I have a list of lists, each sub-list contain exactly 2 floats: my_list = [[0, 1], [0, 1], [0, 1]] Is there a one liner slice operation; so as to not have a for loop? Desired Output: > [[1], [1], [1]] Then, I would like to merge these sub-lists as…
2
votes
1 answer

python reverse string ::-1 slice function equivalent

I see how "String"[::-1] works to return "gnirtS" But isn't this supposed to be compatible with the slice function? "String"[slice(-1, 0, -1)] ? This returns "gnirt"
1 2 3
99
100