Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
-1 votes
3 answers
117 views

How can I get a sublist with wraparound in Python

Simple 1D case I would like to get a substring with wraparound. str = "=Hello community of Python=" # ^^^^^ ^^^^^^^ I want this wrapped substring str[-7] > 'P' str[5]...
Tim Kuipers's user avatar
  • 1,764
0 votes
0 answers
200 views

Extract labels for generated sequences

I am trying to create sequences from a labeled dataset of transactions. The labels are 1 for "fraudulent" and 0 for "genuine", and the label column is called: 'is_fraud'. First, I ...
math189925's user avatar
0 votes
1 answer
61 views

How to extract a new array after skipping a certain number of items in the array repeatly using numpy

num_pixels_per_cell_one_axis = 4 num_cells_per_module_one_axis = 4 inter_cell_sep = 2 max_items_in_list = num_cells_per_module_one_axis * num_pixels_per_cell_one_axis + (num_cells_per_module_one_axis-...
Fahad Rahman's user avatar
1 vote
1 answer
695 views

slicing python list beyond its length

I have to slice a Python list/numpy array from an index to -dx and +dx. where dx is constant. for example: (the position/index that contains 1 only for illustration, as the center index). A = [0, 0, 0,...
tetukowski's user avatar
-2 votes
1 answer
191 views

Formatting columns of multidimensional arrays into 1D arrays [duplicate]

I want to access right side of the 2D array. I would like to turn the second column into a 1D array 4883,4967,4968... . I tried doing list_[len(list_),1] however that doesn't work. How would I be able ...
tony selcuk's user avatar
2 votes
1 answer
146 views

Replacing list comprehensions with pandas and numpy Python

The function below uses slices to get the maximum value between 2 indexes at once. So it gets the maximum value between 0 and 10 and then 10 and 12 and such. The function is derived from the answer to ...
tony selcuk's user avatar
1 vote
2 answers
87 views

Is it possible to slice multidimensional LISTS (not numpy) in python?

This question is the same as this one, but for native python lists. Assume the following: import numpy as np a = np.ones((5, 6, 7)) a_list = a.tolist() I can slice a like so: a_slice = a[2:4, 4:, :3] ...
Gulzar's user avatar
  • 27.7k
0 votes
1 answer
827 views

Slice list with numpy array

I have a list, lets say: list = ['A1', 'A2', 'A3'] I have some data, that returns with an Numpy array of either: array([0]) array([1]) array([2]) Which are indicating the index of the corresponding ...
TooManyRightHands's user avatar
0 votes
1 answer
435 views

Match along last axis in numpy array

I saw a lot of articles and answers to other questions about slicing 3D lists in python, but I can't apply those methods to my case. I have a 3D list: list = [ [[0, 56, 78], [4, 86, 90], [7, 87, ...
zer0flag's user avatar
1 vote
1 answer
1k views

Python Underscore Inside List

I realize that underscores are used for a number of cases, such as throwaway variables in python. I recently came across the following example: a = [1, 2, 5, 3] a[_] Now the second line returns a ...
Convex Leopard's user avatar
0 votes
1 answer
158 views

Why is this Python array not slicing?

Initial data is: array([[0.0417634 ], [0.04493844], [0.04932728], [0.04601787], [0.04511007], [0.04312284], [0.0451733 ], [0.04560687], [0.04263394], [0.04183227], [0....
Fabio G's user avatar
  • 29
1 vote
0 answers
642 views

Slicing to an offset from the end of an array, where the offset may be 0

I'd like to slice an array with an offset from the end, but I'd like to be able to potentially include the end. import numpy as np a = np.arange(10) b = np.zeros_like(a) for i in range(10): b[:-...
K. Nielson's user avatar
1 vote
1 answer
158 views

Slice an array with a list of indices

Suppose a = array([1,2,3]) and b = array([4,5,6]). I want to slice a and b using a list and perform some operation on each part, then return the result to an array. For example i propose a dummy ...
Mikkel Rev's user avatar
3 votes
4 answers
13k views

How to remove empty element from numpy array

Here an example: a = np.array([[1, 2, 3,4], [], [1,2,0,9]]) print(a) # array([list([1, 2, 3, 4]), list([]), list([1, 2, 0, 9])], dtype=object) How to remove the empty element and return only: ...
LearnToGrow's user avatar
  • 1,750
-1 votes
1 answer
30 views

Need to understand what is the use of [::-1] in the code? [duplicate]

I tried to remove the part I got the result but only in reverse order. So my question here is that why are we using that part to splice the array. import numpy as np import cv2 img = cv2.imread("E:/...
Prakhar Nigam's user avatar
3 votes
1 answer
4k views

Slicing a numpy array to select rows with values in a list. Truth value error

I have a 2D numpy array, and I wish to select rows that have a specific value in a given column. This is simple enough to do with single numbers or multiple numbers, but I cannot do it for a list. ...
amquack's user avatar
  • 877
3 votes
3 answers
90 views

Manual slicing of a list using their indices, Python

Minimal example I have a list a = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,....,] I want to get a new list new_list = [40,50,60,100,110,120,...], i.e. append fourth, fifth and sixth value,...
Srivatsan's user avatar
  • 9,363
0 votes
3 answers
270 views

Selecting elements from list of two arrays

I have the following list of two arrays: l=[array([16, 19]), array([24, 17])] How one make new lists to have #[16,24] and [19,17] I have tried l[0] and l[1] but those just give me [array([16, 19]...
Will's user avatar
  • 47
5 votes
3 answers
20k views

what does [::-1] mean in python - slicing?

I have a variable a=range(0,5) and i need to know why and how a[::-1] this works. The answer i am getting is range(4,-1,-1). Thanks for help. P.S. It is quite a basic question but as this question ...
7bStan's user avatar
  • 180
4 votes
2 answers
2k views

How can I slice a list to get every 2 consecutive elements of the list in new lists?

Say you have a list [1,2,3,...,99,100] import numpy as np ls = np.linspace(1,100,100) Say you fiddle around with the slice notation to find ones that works for you. print(ls[:2]) >> [ 1. 2.]...
user avatar
4 votes
5 answers
2k views

Slicing sublists with different lengths

I have a list of lists. Each sublist has a length that varies between 1 and 100. Each sublist contains a particle ID at different times in a set of data. I would like to form lists of all particle IDs ...
Jack's user avatar
  • 131
0 votes
1 answer
6k views

For an array a and b in Python, what is the code a[b[0:2]] is actually trying to do?

Could somebody please help on explaining the meaning of this error message? There is this part of lines of code that I am trying to understand. So I experimented myself with an easier example. I ...
user71346's user avatar
  • 723
3 votes
6 answers
2k views

Python/Numpy fast way to selecting every nth chunk in list

Edited for the confusion in the problem, thanks for the answers! My original problem was that I have a list [1,2,3,4,5,6,7,8], and I want to select every chunk of size x with gap of one. So if I want ...
jonathan chen's user avatar
3 votes
2 answers
4k views

if statement whilst slicing

I have a number of lists that I am slicing, such that (using example data): midpoint = [[0.2], [0.5], [0.6]] Values = [[0.1, 0.3, 0.6, 0.8], [0.2,0.3,0.5,0.7], [0.2,0.5,0.6,0.9]] numbers = numpy....
GeoMonkey's user avatar
  • 1,665
1 vote
1 answer
359 views

How to efficiently slice a Python list into chunks of 20 + a slice for the remainder? [duplicate]

Given a list of URLs urls of unknown length, I want to divide it into slices of 20 urls (plus the slice containing the remainder, if there is one). So if it contains 96 URLS, I'll have a 4 slices of ...
Pyderman's user avatar
  • 16.1k
0 votes
1 answer
120 views

Python - Iterating through list of list as a matrix (slice)

I want to iterate through list of list. To iterate through every list inside list also. list=[[0.9 0.8 0.1 0.2 0.5 ][0.5 0.3 0.2 0.1 0.7 ][0.6 0.1 0.3 0.2 0.9][0.3 0.7 0.4 0.1 0.8]] Thus to iterate ...
Angel's user avatar
  • 11
1 vote
3 answers
1k views

Slice away first element in each (list) element in a list in Python

Say we have a list: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Then I want to create a new list through slicing away the first element in each element in the list: b = a[][1:] Obviously the above doesn'...
Horse SMith's user avatar
  • 1,035
17 votes
2 answers
9k views

Python Slice Notation with Comma/List

I have come across some python code with slice notation that I am having trouble figuring out. It looks like slice notation but uses a comma and a list: list[:, [1, 2, 3]] Is this syntax valid? If ...
Stephan's user avatar
  • 17.9k
6 votes
2 answers
12k views

Converting nested lists of data into multidimensional Numpy arrays

In the code below I am building data up in a nested list. After the for loop what I would like is to cast it into a multidimensional Numpy array as neatly as possible. However, when I do the array ...
Matt's user avatar
  • 952
13 votes
4 answers
10k views

Python lists/arrays: disable negative indexing wrap-around in slices

While I find the negative number wraparound (i.e. A[-2] indexing the second-to-last element) extremely useful in many cases, when it happens inside a slice it is usually more of an annoyance than a ...
wim's user avatar
  • 361k
85 votes
3 answers
65k views

Python Array Slice With Comma?

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(...
SolarLune's user avatar
  • 1,269