Skip to content

Tag: arrays

in Numpy, how to zip two 2-D arrays?

For example I have 2 arrays How can I zip a and b so I get ? Answer You can use dstack: If you must have tuples: For Python 3+ you need to expand the zip iterator object. Please note that this is horribly inefficient:

Reversed array in numpy?

Numpy tentative tutorial suggests that a[ : :-1] is a reversed a. Can someone explain me how we got there? I understand that a[:] means for each element of a (with axis=0). Next : should denote the number of elements to skip (or period) from my understanding. Answer As others have noted, this is a python slic…

Python’s enumerate in Ruby?

Is something built in for this? It doesn’t need to have it’s members immutable, it just needs to be in the standard library. I don’t want to be the guy who subclasses the Array class to add a Python feature to a project. Does it have a different name in Ruby? Answer Something like this in Py…

Add single element to array in numpy

I have a numpy array containing: I want to create an array containing: That is, I want to add the first element on to the end of the array. I have tried the obvious: But I get an error saying ValueError: arrays must have same number of dimensions I don’t understand this – the arrays are both just …

How to replace values at specific indexes of a python list?

If I have a list: And then declare two other lists: How can I take to_modify’s elements as index to indexes, then set corresponding elements in to_modify to replacements, i.e. after running, indexes should be [0,0,3,0,1,0]. Apparently, I can do this through a for loop: But is there other way to do this?…

Immutable numpy array?

Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what’s the minimum set of methods that one has to override to achieve immutability? Answer You can make a numpy array unwriteable: Also see the discussion in this thread: http://mail.scipy.org…