Skip to content
Advertisement

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 slicing technique, and numpy

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 Python: becomes this in

Concatenating two one-dimensional NumPy arrays

How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate: But I get an error: TypeError: only length-1 arrays can be converted to Python scalars Answer Use: The arrays you want to concatenate need to be passed in as a sequence, not as separate arguments. From the NumPy documentation: numpy.concatenate((a1, a2, …), axis=0) Join a sequence of arrays

How does one find the largest consecutive set of numbers in a list that are not necessarily adjacent?

For instance, if I have a list This algorithm should return [1,2,3,4,5,6,7,8,9,10,11]. To clarify, the longest list should run forwards. I was wondering what is an algorithmically efficient way to do this (preferably not O(n^2))? Also, I’m open to a solution not in python since the algorithm is what matters. Thank you. Answer Here is a simple one-pass O(n) solution:

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 1d arrays.

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? Could I use operator.itemgetter somehow? Answer The

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/pipermail/numpy-discussion/2008-December/039274.html and the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html

Advertisement