In python I have an array of arrays:
[[1,2,3,4,5,6,7,8,6,5,4,3],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2]]
And I want to select for each array in array only firs n elements. For example for n == 4 I will get:
[[1,2,3,4],
[3,4,5,6],
[3,4,5,6],
[3,4,5,6],
[3,4,5,6]]
What is the most short code in Python for this?
If you have list of lists then pure python will do:
>>> a = [[1,2,3,4,5,6,7,8,6,5,4,3],
... [3,4,5,6,7,8,9,5,3,2,4,2],
... [3,4,5,6,7,8,9,5,3,2,4,2],
... [3,4,5,6,7,8,9,5,3,2,4,2],
... [3,4,5,6,7,8,9,5,3,2,4,2]]
>>> [x[:4] for x in a]
[[1, 2, 3, 4], [3, 4, 5, 6], [3, 4, 5, 6], [3, 4, 5, 6], [3, 4, 5, 6]]
If it's numpy arrays you can use numpy indexing:
>>> a
array([[1, 2, 3, 4, 5, 6, 7, 8, 6, 5, 4, 3],
[3, 4, 5, 6, 7, 8, 9, 5, 3, 2, 4, 2],
[3, 4, 5, 6, 7, 8, 9, 5, 3, 2, 4, 2],
[3, 4, 5, 6, 7, 8, 9, 5, 3, 2, 4, 2],
[3, 4, 5, 6, 7, 8, 9, 5, 3, 2, 4, 2]])
>>> a[:,:4]
array([[1, 2, 3, 4],
[3, 4, 5, 6],
[3, 4, 5, 6],
[3, 4, 5, 6],
[3, 4, 5, 6]])
You can use list comprehension:
l = [[1,2,3,4,5,6,7,8,6,5,4,3],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2],
[3,4,5,6,7,8,9,5,3,2,4,2]]
result = [el[:4] for el in l]
Related
I have an array as such from a txt file:
[[1, 4, 6, 2, 5]
[3, 4, 3, 5, 4]
[5, 3, 6, 7, 1]]
[[3, 4, 2, 5, 8]
[5, 2, 7, 5, 4]
[4, 2, 4, 4, 0]]
[[2, 5, 3, 1, 4]
[3, 8, 5, 6, 2]
[2, 6, 7, 4, 2]]
I want to take the mean of the 3rd row for each group of results. so each group is in a double bracket [[1, 4, 6, 2, 5]
[3, 4, 3, 5, 4]
[5, 3, 6, 7, 1]] is one group,
[[3, 4, 2, 5, 8]
[5, 2, 7, 5, 4]
[4, 2, 4, 4, 0]] is the second group etc.)
How do I access each group individually?
If you want to take the mean of the third row of each group with:
>>> data[:,2].mean(axis=1)
array([4.4, 2.8, 4.2])
If data is for example:
>>> data
array([[[1, 4, 6, 2, 5],
[3, 4, 3, 5, 4],
[5, 3, 6, 7, 1]],
[[3, 4, 2, 5, 8],
[5, 2, 7, 5, 4],
[4, 2, 4, 4, 0]],
[[2, 5, 3, 1, 4],
[3, 8, 5, 6, 2],
[2, 6, 7, 4, 2]]])
then by slicing, we obtain the third row of each group:
>>> data[:,2]
array([[5, 3, 6, 7, 1],
[4, 2, 4, 4, 0],
[2, 6, 7, 4, 2]])
Then for each row we can calculate the mean with .mean(axis=1)
If your data is like this, This code will work fine
lst =[
[[1, 4, 6, 2, 5],[3, 4, 3, 5, 4],[5, 3, 6, 7, 1]],
[[3, 4, 2, 5, 8],[5, 2, 7, 5, 4],[4, 2, 4, 4, 0]],
[[2, 5, 3, 1, 4],[3, 8, 5, 6, 2],[2, 6, 7, 4, 2]]
]
import numpy as np
means = []
for i in lst:
means.append(np.mean(i[2]))
print(means)
>> [4.4, 2.8, 4.2]
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
rotated_map = []
for i in range(len(m[0])):
rotated_map.append([x[i] for x in m])
print(rotated_map)
"""
my result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
desired result = [[8,7,2,3],
[1,5,3,6],
[9,4,3,7],
[5,2,6,1]]
"""
I am trying to rotate the list by putting all the last elements first from the lists into one list then the second to last element into another and so on until i get to the first element.
Transpose the list with zip, then reverse it with the [::-1] syntax.
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> list(map(list, zip(*m)))[::-1]
>>> [[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
edit:
If you want pretty printing, it's probably easiest to use numpy arrays all the way.
>>> import numpy as np
>>>
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> m = np.array(m)
>>> m
>>>
array([[5, 9, 1, 8],
[2, 4, 5, 7],
[6, 3, 3, 2],
[1, 7, 6, 3]])
>>>
>>> m.T[::-1]
>>>
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
Note that m and m.T[::-1] share the same data, because m.T[::-1] is just another view of m. If you need to duplicate the data, use
result = m.T[::-1].copy()
You could use zip, unpacking your list of lists with the *, and inversing the result with [::-1]:
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
res = [list(i) for i in zip(*m)][::-1]
>>> res
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If numpy is an option, transposing is easier:
import numpy as np
>>> np.transpose(m)[::-1]
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
# or:
>>> np.flip(np.transpose(m),0)
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
You can use numpy module to do it. It has the property to transpose the array. Check the below code:
import numpy as np
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
arr = np.array(m).transpose()
new_list = []
for i in range(arr.shape[0]-1,-1,-1):
new_list.append(list(arr[i]))
print(new_list)
Output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If you want to rotate the list clockwise:
list(map(list, zip(*m[::-1])))
Else, for anti-clockwise:
list(map(list, zip(*m)))[::-1]
use the reverse keyword
result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
result.reverse()
print(result)
output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
I m a little new to python. I have a function named featureExtraction which returns a 1-D array for an image. I need to stack all such 1-d arrays row wise to form a 2-d array. I have the following equivalent code in MATLAB.
I1=imresize(I,[256 256]);
Features(k,:) = featureextraction(I1);
featureextraction returns a 1-d row vector which is stacked row-wise to form a 2-d array. What is the equivalent code snippet in python?
Thank You in advance.
Not sure what you're looking for, but maybe vstack or column_stack?
>>> np.vstack((a,a,a))
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> np.column_stack((a,a,a))
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9]])
Or even just np.array:
>>> np.array([a,a,a])
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
You can use numpy.vstack():
a = np.array([1,2,3])
np.vstack((a,a,a))
#array([[1, 2, 3],
# [1, 2, 3],
# [1, 2, 3]])
Let's say I have the following pattern:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and I want to use this to create the following:
PATTERN | HORIZONTAL_MIRROR (PATTERN)
VERTICAL_MIRROR(PATTERN) | HORIZONTAL_MIRROR(VERTICAL_MIRROR(PATTERN))
In other words:
[[1, 2, 3, 3, 2, 1],[4, 5, 6, 6, 5, 4],[7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
is there an efficient method that can be used in Python apart from copying each element from the PATTERN?
Just an idea:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
def patternify(l):
for sl in l:
yield sl+sl[::-1]
for sl in l[::-1]:
yield sl+sl[::-1]
list(patternify(PATTERN))
#output: [[1, 2, 3, 3, 2, 1], [4, 5, 6, 6, 5, 4], [7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
If I understood the requirement correctly:
def mirror(pat):
return pat + pat[::-1]
SUPERPATTERN = mirror(map(mirror, PATTERN))
I am working in Python and I have a NumPy array like this:
[1,5,9]
[2,7,3]
[8,4,6]
How do I stretch it to something like the following?
[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]
These are just some example arrays, I will actually be resizing several sizes of arrays, not just these.
I'm new at this, and I just can't seem to wrap my head around what I need to do.
#KennyTM's answer is very slick, and really works for your case but as an alternative that might offer a bit more flexibility for expanding arrays try np.repeat:
>>> a = np.array([[1, 5, 9],
[2, 7, 3],
[8, 4, 6]])
>>> np.repeat(a,2, axis=1)
array([[1, 1, 5, 5, 9, 9],
[2, 2, 7, 7, 3, 3],
[8, 8, 4, 4, 6, 6]])
So, this accomplishes repeating along one axis, to get it along multiple axes (as you might want), simply nest the np.repeat calls:
>>> np.repeat(np.repeat(a,2, axis=0), 2, axis=1)
array([[1, 1, 5, 5, 9, 9],
[1, 1, 5, 5, 9, 9],
[2, 2, 7, 7, 3, 3],
[2, 2, 7, 7, 3, 3],
[8, 8, 4, 4, 6, 6],
[8, 8, 4, 4, 6, 6]])
You can also vary the number of repeats for any initial row or column. For example, if you wanted two repeats of each row aside from the last row:
>>> np.repeat(a, [2,2,1], axis=0)
array([[1, 5, 9],
[1, 5, 9],
[2, 7, 3],
[2, 7, 3],
[8, 4, 6]])
Here when the second argument is a list it specifies a row-wise (rows in this case because axis=0) repeats for each row.
>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> numpy.kron(a, [[1,1],[1,1]])
array([[1, 1, 5, 5, 9, 9],
[1, 1, 5, 5, 9, 9],
[2, 2, 7, 7, 3, 3],
[2, 2, 7, 7, 3, 3],
[8, 8, 4, 4, 6, 6],
[8, 8, 4, 4, 6, 6]])
Unfortunately numpy does not allow fractional steps (as far as I am aware). Here is a workaround. It's not as clever as Kenny's solution, but it makes use of traditional indexing:
>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> step = .5
>>> xstop, ystop = a.shape
>>> x = numpy.arange(0,xstop,step).astype(int)
>>> y = numpy.arange(0,ystop,step).astype(int)
>>> mg = numpy.meshgrid(x,y)
>>> b = a[mg].T
>>> b
array([[1, 1, 5, 5, 9, 9],
[1, 1, 5, 5, 9, 9],
[2, 2, 7, 7, 3, 3],
[2, 2, 7, 7, 3, 3],
[8, 8, 4, 4, 6, 6],
[8, 8, 4, 4, 6, 6]])
(dtlussier's solution is better)