Home > OS >  Flattening a 2d list into 1d list in python
Flattening a 2d list into 1d list in python

Time:10-15

I have a list of 2D elements

m = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]

and I want my output to be:

1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20

I tried this loop:

for i in range(3):
    for k in range(i,len(m),3):
        print(*m[i][k:k 3],sep='\t')

but it prints

1 2 3
4 5
6 7 8
9 10
11 12 13
14 15
16 17 18

and gives me an error

I'm not sure if it is possible since it is going on the next element. Can anyone help me on this?

CodePudding user response:

You can use this snippet

m = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]

flag=0

for i in range(len(m)):

for j in range(len(m[i])):
    if(flag==3):
        print()
        flag=0
    print(m[i][j],end=" ")
    flag =1

I think it may work out Thanks !!!

CodePudding user response:

An approach like this would work:

m = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]

curr = 0
for i in m:
  for j in i:
    curr  = 1
    if(curr % 3 == 0):
      print(j)
    else:
      print(j, end = ' ')

Output:

1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 

You can create a variable, curr, to act as a counter variable. Then, iterate through every element of m, and increment curr with each iteration. For every third element, given by curr % 3 == 0%, we print an element WITH a newline. For every not-third element, we print the element without a newline.

I hope this helped! Please let me know if you have any further questions or clarifications :)

CodePudding user response:

import itertools
x = list(itertools.chain(*m))
print([x[i:i 3] for i in range(0,len(x),3)])

Of course, the above will print the whole thing as a list of lists, but you can go from there to printing each of the individual sublists.

CodePudding user response:

one-line version:

print("".join([str(e) " " if e%3!=0 else str(e) "\n" for row in m for e in row]))

P.S. m = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]] as that of OP.


easy-to-read version:

m does not need to be identical to that of OP. Could be any 2d matrix.

flat = [e for row in m for e in row]
for i in range(len(flat)):
    if i%3 != 2 : print(flat[i], end = " ")
    else : print(flat[i], end = "\n")
if len(flat)%3 != 0: print("") 

CodePudding user response:

I would try something like

count = 0
for arr in matrix:
    for num in arr:
        print(num, end=' ')
        count  = 1
        if count == 3:
            print()
            count = 0

CodePudding user response:

itertools.chain will effectively combine all the sublists into one, and more_itertools.chunked will break it up into equal-sized segments.

The asterisks are for unpacking; in this case print(*triplet) is effectively the same as print(triplet[0], triplet[1], triplet[2]), and print automatically inserts a space between multiple arguments by default.

from itertools import chain
from more_itertools import chunked

m = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]]

for triplet in chunked(chain(*m), 3):
    print(*triplet)
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
  • Related