3

I need help creating a for loop or suggestions on how to do this better. I have an empty list of 0's based on number of rows and columns. Then another list that contains the data.

I wrote down manually how to change the values to visualize it.

r = 2
c = 2
all_list = [[0 for x in range(c*3)] for y in range(3*r)]
a = [[[' ', 55, ' '], [57, 0, 63], [' ', 43, ' ']], [[' ', 71, ' '], [95, 1, 64], [' ', 37, ' ']], [[' ', 80, ' '], [12, 2, 49], [' ', 69, ' ']], [[' ', 63, ' '], [54, 3, 17], [' ', 84, ' ']]]

#Need to convert the list above to this.
#[[' ', 55, ' ', ' ', 71, ' '], [57, 0, 63, 95, 1, 64], [' ', 43, ' ', ' ', 37, ' '], [' ', 80, ' ', ' ', 63, ' '], [12, 2, 49, 54, 3, 17], [' ', 69, ' ', ' ', 84, ' ']]

all_list[0][0] = a[0][0][0]
all_list[0][1] = a[0][0][1]
all_list[0][2] = a[0][0][2]
all_list[0][3] = a[1][0][0]
all_list[0][4] = a[1][0][1]
all_list[0][5] = a[1][0][2]

all_list[1][0] = a[0][1][0]
all_list[1][1] = a[0][1][1]
all_list[1][2] = a[0][1][2]
all_list[1][3] = a[1][1][0]
all_list[1][4] = a[1][1][1]
all_list[1][5] = a[1][1][2]

all_list[2][0] = a[0][2][0]
all_list[2][1] = a[0][2][1]
all_list[2][2] = a[0][2][2]
all_list[2][3] = a[1][2][0]
all_list[2][4] = a[1][2][1]
all_list[2][5] = a[1][2][2]

all_list[3][0] = a[2][0][0]
all_list[3][1] = a[2][0][1]
all_list[3][2] = a[2][0][2]
all_list[3][3] = a[3][0][0]
all_list[3][4] = a[3][0][1]
all_list[3][5] = a[3][0][2]

all_list[4][0] = a[2][1][0]
all_list[4][1] = a[2][1][1]
all_list[4][2] = a[2][1][2]
all_list[4][3] = a[3][1][0]
all_list[4][4] = a[3][1][1]
all_list[4][5] = a[3][1][2]

all_list[5][0] = a[2][2][0]
all_list[5][1] = a[2][2][1]
all_list[5][2] = a[2][2][2]
all_list[5][3] = a[3][2][0]
all_list[5][4] = a[3][2][1]
all_list[5][5] = a[3][2][2]
print(all_list)

Here is what I have so far:

  for i in range(len(all_list)):
        for j in range(r*c):
            for k in range(r):
                for t in range(3):
                    all_list[i][j] = a[][k][t] #This is not correct
Abhi
  • 65
  • 5
  • so is a a 3 dimensional list 4x3x3 and you want to reshape it into a 4x6 matrix? – Garrett R Feb 23 '16 at 23:48
  • @Seekheart To print the list like this: `s='\n'.join([' '.join([str(item) for item in row]) for row in all_list]) print(s)` – Abhi Feb 23 '16 at 23:49

3 Answers3

4

This works:

firstHalf = [thing[0]+thing[1] for thing in zip(*a)]
secondHalf = [thing[2]+thing[3] for thing in zip(*a)]
reshaped = firstHalf + secondHalf

Output

[[' ', 55, ' ', ' ', 71, ' '],
 [57, 0, 63, 95, 1, 64],
 [' ', 43, ' ', ' ', 37, ' '],
 [' ', 80, ' ', ' ', 63, ' '],
 [12, 2, 49, 54, 3, 17],
 [' ', 69, ' ', ' ', 84, ' ']]
Garrett R
  • 2,687
  • 11
  • 15
  • Do you know why it only prints firstHalf when print is called on reshaped? – Abhi Feb 24 '16 at 00:32
  • It prints the entire list (of lists) for me. Are you using python 2.7.x or 3.x – Garrett R Feb 24 '16 at 00:35
  • I am using 3.x. I am able to print the entire list if I add `zipped = zip(*a)` again after firstHalf =. – Abhi Feb 24 '16 at 01:12
  • Yep! In python 3.x zip returns an iterator rather than a list (meaning you can only iterate over it once!). Fixed for compatibility – Garrett R Feb 24 '16 at 01:19
  • If I want to make this into a loop irrespective of range(4) or only two things in a thing for zip(*a). Am I on the correct path? `for thing in zip(*a): for i in range(2): firstHalf = [thing[i]+thing[i+1] for thing in zip(*a)]` – Abhi Feb 24 '16 at 01:50
  • Well, you want to jump from 0,2,4. So you probably want range(start, end, 2) – Garrett R Feb 24 '16 at 01:51
  • You might also consider accepting the solution if it works for you – Garrett R Feb 25 '16 at 01:41
1

From an answer I wrote a long time ago:

def nested_loop(n, l):
    for c in range(l ** n):
        yield tuple(c // l**x % l for x in reversed(range(n)))

Use it like this, for your case:

for (i, j) , (l, k, m) in zip(nested_loop(2, 6), nested_loop(3, 3)):
    all_list[i][j] = a[k][l][m]

edit: I just noticed k is not quite right, trying to fix it.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
-1

I can see that i and j are independently incrementing from k & t. So I will prefer to write another function to take care of incrementing i& j.

like

incrementIandJ(i,j)
{
if (J==5){i++;j=0)
}

And write 3 for loops for changing index of a[][][]; like

for l in range (len(a))
   for k in range ...
      for t in range ....{
            all_list[i][j]  = a[l][k][t];
            incrementIandJ(i,j);
            }

Hope this will answer your question. May not be the best optimized in terms of performance, But I infer its not a matter of concern here.

  • 1
    It looks like you have coded more in C than in Python. In Python, you don't need the braces or the semicolons. Also, `i++` is invalid syntax. You need a colon after your `if` statement, and it needs to be indented. The commands in your `if` statement should be on new lines and indented. – zondo Feb 23 '16 at 23:53