For example, if my original nested list is :
[[' ', 'Y', ' ', 'X'],
['S', ' ', 'V', ' '],
['T', 'X', 'Y', 'S'],
['X', ' ', 'X', 'Y']]
I would like the result to be:
[[' ', ' ', ' ', ' '],
['S', ' ', 'V', 'X'],
['T', 'Y', 'Y', 'S'],
['X', 'X', 'X', 'Y']]
So basically drop all the letters to the bottom without getting rid of any of the spaces and maintaining the structure of the list.
Here is what I have so far. My result gives me this which is incorrect.
[[' ', ' ', ' ', ' '],
['S', 'Y', 'V', 'X'],
['T', ' ', 'Y', 'S'],
['X', 'X', 'X', 'Y']]
def find_letters(m,constant):
try:
for i in range(len(m[constant])):
if m[constant][i] == ' ':
if m[constant - 1][i] != ' ':
m[constant][i] = m[constant - 1][i]
m[constant - 1][i] = ' '
find_letters(m,constant - 1)
except IndexError:
pass
l = [[' ', 'Y', ' ', 'X'],
['S', ' ', 'V', ' '],
['T', 'X', 'Y', 'S'],
['X', ' ', 'X', 'Y']]
constant = -1
(find_letters(l,constant))