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
5 answers

pandas multiple slicers of MultiIndex

I want to select multiple columns from a MultiIndex with multiple independent indexers. For example df = pd.DataFrame( np.zeros((2,4)), columns=pd.MultiIndex.from_product([('a','b'),(1,2)]) ) from this DataFrame a b 1 2 …
goweon
  • 1,111
  • 10
  • 19
2
votes
2 answers

Numpy variable slice size (possibly zero)

Lets say I've got some time series data: import numpy as np import matplotlib.pyplot as plt np.random.seed(42) x = np.linspace(0, 10, num=100) time_series = np.sin(x) + np.random.random(100) plt.plot(x, time_series) If I want to 'delay' the time…
beyarkay
  • 513
  • 3
  • 16
2
votes
2 answers

DICOM slice thickness in Python

I'm working on a project that requires slicing a 3D .stl file into a stack of DICOM slices, and I need to get the distance between the slices. I have figured out how to include the real .stl dimensions (in mm) in each DICOM file, using Autodesk…
Nico
  • 75
  • 5
2
votes
1 answer

How to check if an object can slice

Is this the right way to check if a Python object can be sliced or are there better/correct ways? import typing def can_slice(object) -> bool: return "__getitem__" in dir(object) I am looking for a way not using exception to test if an object…
mon
  • 18,789
  • 22
  • 112
  • 205
2
votes
0 answers

Slicing by non-integers and type checking

I am working with time series, and want to retrieve and slice by date/time. Ideally, I'd leverage the existing [:] syntax. For example (see last line): from datetime import datetime from typing import Union class T: def __getitem__(self, key:…
Oblomov
  • 55
  • 4
2
votes
2 answers

Is it possible to only update "see more" inside of map()?

I am practicing a project using React.js as a front-end framework to display the products. I tried to filter the product by the product_type. When fetch the data, I use slice() to show 3 products on each category then if the products in the category…
2
votes
1 answer

What to use instead of `std::lower_bound` and `std::upper_bound` in Rust?

What we have in C++ C++ has two STL functions: std::lower_bound and std::upper_bound std::lower_bound finds first position of searched value if it exists, or position of first value greater. std::upper_bound finds first position with greater value…
2
votes
1 answer

Putting text file into slice then compare

I am writing a program where I take usernames line by line in a .txt file and verify that the username is in it. I have then created a slice and converted the file into a string and appended it to the string slice. I'm now trying to loop over the…
2
votes
4 answers

Set variable column values to nan based on row condition

I want to be able to variably change a column value based on the value of the first column. Say I have a dataframe as follows: col_ind col_1 col_2 col_3 3 a b c 2 d e f 1 g h i …
2
votes
2 answers

C# performance - pointer to span in a hot loop

I'm looking for a faster alternative to BitConverter: But! Inside a "hot loop": //i_k_size = 8 bytes while (fs.Read(ba_buf, 0, ba_buf.Length) > 0 && dcm_buf_read_ctr < i_buf_reads) { Span sp_data = ba_buf.AsSpan(); for (int i = 0; i <…
2
votes
1 answer

Recursive slice type definition. type N []N

I stumbled upon a recursive type definition in this question: type N []N As I understand, it defines a type with a name N that is a slice of an array of type N, meaning this type definition is recursive. What is the rationale behind treating this…
2
votes
0 answers

How does Golang GC reclaim complete allocations when user references don't point to the start of the allocation?

How does the Go garbage collector know the beginning address of a slice's backing store, if the slice points to the middle of the original store? Consider the following: array := make([]int, 1000) slice := array[999:1000] array = nil We are…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
2
votes
1 answer

Numpy. How to split 2D array to multiple arrays by grid?

I have a numpy 2D-array: c = np.arange(36).reshape(6, 6) [[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]] I want to split it to…
Massimo
  • 836
  • 3
  • 17
  • 38
2
votes
2 answers

How can I copy a u8 slice into a u32 slice?

When you have two slices of the same type (and length!), Rust provides the copy_from_slice function to copy from one to the other. But if my source slice is a &[u8] and my target slice is a &[u32], is there any simpler way to copy it than manually…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
2
votes
3 answers

Do sending more than 8 element to a channel can result in deadlock?

package main import ( "fmt" "time" ) func printCount(c chan int) { num := 0 for num >= 0 { num = <-c fmt.Print(num, " ") } } func main() { a := []int{8, 6, 7, 5, 3, 0, 9, -1, 3, 4} // If i use a :=…