Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# How to reshape python list without numpy

By : I'm greatfull
Date : October 17 2020, 06:10 AM
hope this fix your issue How do I reshape a list into a n-dimensional list , This recursive approach should work.
code :
``````lst = [1, 2, 3, 4, 5, 6, 7, 8]
shape = [2, 2, 2]

from functools import reduce
from operator import mul

def reshape(lst, shape):
if len(shape) == 1:
return lst
n = reduce(mul, shape[1:])
return [reshape(lst[i*n:(i+1)*n], shape[1:]) for i in range(len(lst)//n)]

reshape(lst, shape)
``````
``````assert reduce(mul, shape) == len(lst)
``````

Share :

## Python numpy: reshape list into repeating 2D array

By : Naveen Basavaraj
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further numpy.meshgrid will do this for you.
N.B. From your requested output, it looks like you want ij indexing, not the default xy
code :
``````from numpy import meshgrid

x = [0,1,2,3]
y = [4,5,6,7]
xx,yy=meshgrid(x,y,indexing='ij')

print xx
>>> [[0 0 0 0]
[1 1 1 1]
[2 2 2 2]
[3 3 3 3]]

print yy
>>> [[4 5 6 7]
[4 5 6 7]
[4 5 6 7]
[4 5 6 7]]
``````
``````xx,yy=meshgrid(x,y,indexing='xy')

print xx
>>> [[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]

print yy
>>> [[4 4 4 4]
[5 5 5 5]
[6 6 6 6]
[7 7 7 7]]
``````

## Numpy reshape sub-list

By : Eric Smith
Date : March 29 2020, 07:55 AM
I wish did fix the issue. NumPy is an n-dimensional array library, not a matrix library. 1D arrays don't have rows.
If you want a view of an arbitrary array with an extra length-1 axis stuck on the end, you can do that:
code :
``````train_y = train_y[..., np.newaxis]
# or
train_y = train_y.reshape(train_y.shape + (1,))
``````

## Python numpy how to reshape this list of arrays/images into a collage?

By : sivas
Date : March 29 2020, 07:55 AM
it fixes the issue I've got the following list of 25 mini black-and-white images representing patterns: , Solution:
code :
``````imgs.reshape(5, 5, 3, 3, 1).swapaxes(1, 2).reshape(15, 15, 1)
``````
``````# test data
# each 3x3 image consists of the 9 identical digits

A = np.stack([
np.full((3, 3, 1), i)
for i in range(1, 26)
])

with_swap = A.reshape(5, 5, 3, 3, 1).swapaxes(1, 2).reshape(15, 15, 1)
print(with_swap[...,-1])

without_swap = A.reshape(15, 15, 1)
print(without_swap[...,-1])
``````
``````[[ 1  1  1  2  2  2  3  3  3  4  4  4  5  5  5]
[ 1  1  1  2  2  2  3  3  3  4  4  4  5  5  5]
[ 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 10 10 10]
[ 6  6  6  7  7  7  8  8  8  9  9  9 10 10 10]
[ 6  6  6  7  7  7  8  8  8  9  9  9 10 10 10]
[11 11 11 12 12 12 13 13 13 14 14 14 15 15 15]
[11 11 11 12 12 12 13 13 13 14 14 14 15 15 15]
[11 11 11 12 12 12 13 13 13 14 14 14 15 15 15]
[16 16 16 17 17 17 18 18 18 19 19 19 20 20 20]
[16 16 16 17 17 17 18 18 18 19 19 19 20 20 20]
[16 16 16 17 17 17 18 18 18 19 19 19 20 20 20]
[21 21 21 22 22 22 23 23 23 24 24 24 25 25 25]
[21 21 21 22 22 22 23 23 23 24 24 24 25 25 25]
[21 21 21 22 22 22 23 23 23 24 24 24 25 25 25]]
``````
``````[[ 1  1  1  1  1  1  1  1  1  2  2  2  2  2  2]
[ 2  2  2  3  3  3  3  3  3  3  3  3  4  4  4]
[ 4  4  4  4  4  4  5  5  5  5  5  5  5  5  5]
[ 6  6  6  6  6  6  6  6  6  7  7  7  7  7  7]
[ 7  7  7  8  8  8  8  8  8  8  8  8  9  9  9]
[ 9  9  9  9  9  9 10 10 10 10 10 10 10 10 10]
[11 11 11 11 11 11 11 11 11 12 12 12 12 12 12]
[12 12 12 13 13 13 13 13 13 13 13 13 14 14 14]
[14 14 14 14 14 14 15 15 15 15 15 15 15 15 15]
[16 16 16 16 16 16 16 16 16 17 17 17 17 17 17]
[17 17 17 18 18 18 18 18 18 18 18 18 19 19 19]
[19 19 19 19 19 19 20 20 20 20 20 20 20 20 20]
[21 21 21 21 21 21 21 21 21 22 22 22 22 22 22]
[22 22 22 23 23 23 23 23 23 23 23 23 24 24 24]
[24 24 24 24 24 24 25 25 25 25 25 25 25 25 25]]
``````

## How does the numpy.resize and numpy.reshape function works internally in python ?

By : 陶仁彥
Date : March 29 2020, 07:55 AM
To fix the issue you can do Neither interpolates. And if you are wondering about interpolation and pixels of an image, they probably aren't the functions that you want. There some image packages (e.g in scipy) that manipulate the resolution of images.
Every numpy array has a shape attribute. reshape just changes that, without changing the data at all. The new shape has to reference the same total number of elements as the original shape.

## Python numpy reshape 3d list into 2d array

By : AkSingh
Date : March 29 2020, 07:55 AM
Hope this helps I wish to convert , Reshape is your tool.
Here is a self contained example:
code :
``````import numpy as np
a = np.array([
[[[1,2,3] , [4,5,6]],
[[7,8,9] , [10,11,12]]],
[[[13,14,15] , [16,17,18]],
[[19,20,21] , [22,23,24]]]
])

a.shape
>>> (2, 2, 2, 3)

a.reshape(8,3)
>>> array([[ 1,  2,  3],
>>>        [ 4,  5,  6],
>>>        [ 7,  8,  9],
>>>        [10, 11, 12],
>>>        [13, 14, 15],
>>>        [16, 17, 18],
>>>        [19, 20, 21],
>>>        [22, 23, 24]])
``````