Linked Questions
16 questions linked to/from How can I use Python built-in slice object?
19 votes
2 answers
11k views
Python pass slice as argument [duplicate]
I want to pass a slice to a function so I can select a portion of a list. Passing as a string is preferable as I will read the required slice as a command line option from the user. def ...
4 votes
1 answer
1k views
Can I store slicers in a variable? (Pandas/Python) [duplicate]
I'm just wondering if I can do something like: df.loc['1990':'2000'] by doing something like: my_slice = '1990':'2000' df.loc[my_slice] What I've written doesn't work, but is there something similar ...
2 votes
1 answer
792 views
How to use tuples as slice start:end values [duplicate]
I need to slice array with arbitrary dimensions by two tuples. I can use slicing, such as a[1:3, 4:6]. But what do i do if 1,3 and 4,6 are tuples? While a[(1, 3)] works, I tried a[(1, 3), (4, 6)] and ...
1 vote
1 answer
255 views
Subset a string based on variable [duplicate]
Suppose we have a long string, for example: s = "The long-string instrument is a musical instrument in which the string is of such a length that the fundamental transverse wave is below what a person ...
4694 votes
38 answers
3.2m views
How slicing in Python works
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? See Why are slice and range upper-bound ...
147 votes
5 answers
117k views
Implementing slicing in __getitem__
I am trying to implement slice functionality for a class I am making that creates a vector representation. I have this code so far, which I believe will properly implement the slice but whenever I do ...
11 votes
2 answers
4k views
How can I use colon (:) in variable [duplicate]
I want to write code like this: index = 0:2 print(list[index]) but this does not work. Is there any way I can store all parts of the [...:...] syntax in a variable?
13 votes
2 answers
25k views
ValueError: Shape mismatch: if categories is an array, it has to be of shape (n_features,)
I have create a simple code to implement OneHotEncoder. from sklearn.preprocessing import OneHotEncoder X = [[0, 'a'], [0, 'b'], [1, 'a'], [2, 'b']] onehotencoder = OneHotEncoder(categories=[0]) X = ...
8 votes
2 answers
18k views
Pandas how does IndexSlice work
I am following this tutorial: GitHub Link If you scroll down (Ctrl+F: Exercise: Select the most-reviewd beers ) to the section that says Exercise: Select the most-reviewd beers: The dataframe is ...
3 votes
3 answers
305 views
How does work this pythonic trick: a[::-1]
>> a = range(10) >> print a[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] This slice gives reversed list. How does it work?
3 votes
1 answer
614 views
How can I provide syntactic sugar for slicing a numpy array?
I would like to write more readable code by doing something like: import numpy as np SLICE_XY = slice(0, 2) SLICE_Z = slice(2, 3) data = np.array([[0, 1, 2], [3, 4, 5], ...
0 votes
1 answer
322 views
Tokenize string with field of specific length in pyparsing
I'm writing a simple parser for ascii data, in which each row has to be interpreted as fields of 8-block of chars: """ |--1---||--2---||--3---||--4---||--5---||--6---||--7---||--8---||--9---| GRID ...
-1 votes
3 answers
167 views
How to invert a list of strings left to right [duplicate]
I have a bunch of lists and I want to inverse their string content from left to right. How to transform x x = ['TARDBP', 'BUB3', 'TOP2A', 'SYNCRIP', 'KPNB1'] to x = ['KPNB1', 'SYNCRIP', 'TOP2A', '...
0 votes
2 answers
66 views
How to check if second value of slice() is a certain value?
I have a function that takes in a slice. What I want to do is to check if the end value in this slice is equal to -1. If that is the case, I want to reset the slice to another value. I can't find ...
0 votes
1 answer
52 views
Python Regex for finding the name and the following numbers multiple times
I need to parse an output text file that has a lot of information, specifically about weighing and calibrating masses. There is a data table in this text file that has the name of the mass being ...