Linked Questions

19 votes
2 answers
11k views

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 ...
Sandy's user avatar
  • 211
4 votes
1 answer
1k views

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 ...
cjm2671's user avatar
  • 19.6k
2 votes
1 answer
792 views

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 ...
Ford F150 Gaming's user avatar
1 vote
1 answer
255 views

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 ...
gregoruar's user avatar
  • 383
4694 votes
38 answers
3.2m views

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 ...
Simon's user avatar
  • 81.2k
147 votes
5 answers
117k views

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 ...
nicotine's user avatar
  • 2,679
11 votes
2 answers
4k views

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?
giorgi shengelaia's user avatar
13 votes
2 answers
25k views

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 = ...
arga wirawan's user avatar
8 votes
2 answers
18k views

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 ...
Cheng's user avatar
  • 18.1k
3 votes
3 answers
305 views

>> 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?
Oleg Razgulyaev's user avatar
3 votes
1 answer
614 views

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], ...
McManip's user avatar
  • 300
0 votes
1 answer
322 views

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 ...
Luciana Pilloni's user avatar
-1 votes
3 answers
167 views

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', '...
J.A's user avatar
  • 214
0 votes
2 answers
66 views

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 ...
Rivered's user avatar
  • 799
0 votes
1 answer
52 views

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 ...
Peter Wang's user avatar
  • 1,848

15 30 50 per page