All Questions
66 questions
6votes
5answers
514views
Any way to speedup itertool.product
I am using itertools.product to find the possible weights an asset can take given that the sum of all weights adds up to 100. min_wt = 10 max_wt = 50 step = 10 nb_Assets = 5 weight_mat = [] for i ...
4votes
3answers
311views
How do I write many nested for-loops in a few lines of Python code?
What I have: I have the list List123=[-13,3,12,1] and the 2-by-4-matrix Matrix123=numpy.zeros((2,4), dtype=Decimal). What I want: I want to change all entries of the matrix to any entry of the list ...
3votes
3answers
582views
How to speed up nested loop of lookup operations?
I am programming the halftoning of images for laser-engraving. At a given setting, the laser only turns on or off, so I can give it binary images with 1-bit depth. So I convert grayscale images with 8-...
2votes
3answers
219views
How to avoid nested for loops in NumPy?
I have this code. n_nodes = len(data_x) X = np.zeros((n_nodes, n_nodes)) for i in range(n_nodes): for j in range(n_nodes): X[i, j] = data_x[i] ** j I want to do the same task with no loops ...
2votes
2answers
4kviews
Optimizing a nested-loop operation
I have this datacube containing data for each pixel of an image (pretty much like hyperspectral imaging). I'm trying to fit a line on each pixel of the image, in an efficient way. Right now, I do it ...
2votes
3answers
2kviews
Using nested for loops to evaluate function at every coordinate - python
I am trying to use a nested for loop to evaluate function f(r) at every combination of 2D polar coordinates. My current code is: r = np.linspace(0, 5, 10) phi = np.linespace(0,2*np.pi, 10) for i in ...
2votes
4answers
1kviews
How to rewrite this nested for loop in python? [duplicate]
I have a nested for loop, and I'm doing some operations on it, however it's take about 20 minutes to run. Hoping to reduce the wall time. This is just a simple example to make a reproducible one, but ...
2votes
2answers
2kviews
Removing nested loops in numpy
I've been writing a program to brute force check a sequence of numbers to look for euler bricks, but the method that I came up with involves a triple loop. Since nested Python loops get notoriously ...
2votes
1answer
2kviews
Vectorizing operations in nested loops: Python
I have 2D numpy array, I need two nested loops to iterate over each of its elements. I want to make some vectorization on the nested loops, but I keep getting an error saying, j= np.arange (0,x....
2votes
1answer
99views
Is there a smart way to vectorize a nested for-loop where the inner index is limited by the outer index?
Is there a smart way to vectorize a nested for loop of inner products, where the inner index is lower bound by the outer index? Here's a simple example. Say that arr1 and arr2 are numpy arrays each ...
2votes
1answer
52views
Replace for loops that exchange attributes of objects with numpy function
I made a bouncing ball simulation in Matplotlib, the update function exchange the move vectors (attributes) of the balls when they collide. Is it possible to replace the for loops in the update ...
2votes
1answer
3kviews
How to extract values from a python list or numpy array using conditional checks by the application of numpy vectorization?
I have the following code and I want to extract certain values from other lists that depends upon the given condition. But my data sets are huge ~ 1 million values in each list. Therefore this method ...
2votes
2answers
87views
Python: Creating an N-dimensional list from separate lists of varying sizes and dtypes
Say I have these lists: a = [1, 2, 3, 4] b = [6,7] c = ['a', 'b', 'c'] I would like to create a 3-Dimensional data structure that contains new lists combining all the elements of each list together ...
2votes
1answer
938views
Removing nested loops within python
I am trying to speed up my code. The biggest problem is a few nested loops I have (they have to iterate over 25000 cells). However, when I try to get rid of these nested loops, I get a different ...
2votes
1answer
416views
How can I speed up iteration through this transformed numpy array?
Below is a partial class definition for a map of 2D isometric tiles to be rendered using pyglet. class Map(object): origin = 0 drytile = tile.dry wettile = tile.wet def __init__(...