0

I have an array of 200 items.

grid = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .... and so on]

How can I print it as a 2D array like so? Split it every 10 characters actually.

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

I have tried things such as

def Print(grid):
    for i in grid:
        if(len(i) % WIDTH-1 == 0):
            print(i)
        else:
            print(i, end = ' ')

with no success.

1
  • So every 10 characters is a column? What if the grid does not evenly divide by 10? Commented Feb 11, 2021 at 17:18

8 Answers 8

2

Here is a simple solution using slicing:

a = [0 for i in range(200)]

for i in range(len(a) // 10):
    sub_list = a[i * 10:(i + 1) * 10]
    print(', '.join(map(str, sub_list)))

Although this can be compressed to a single line with some list comprehensions, it would then become very hard to understand.

Sign up to request clarification or add additional context in comments.

Comments

1

You can iterate of the length of grid, in this case 200 by 10 and use that as the start for your slicing, printing that plus 9 additional values.

grid = [0]*200
for x in range(0,len(grid),10):
    print(grid[x:x+9])

edit: This will print slices of the list, which is still a list which is why you see the brackets. Like others have shown you can use join and map to print it as a comma separated string if you really need to:

grid = [0]*200
for x in range(0,len(grid),10):
    print(','.join(map(str,grid[x:x+9])))

Output

[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]

4 Comments

That is great. Any ideas why it is also printing brackets and commas, besides the values?
@IAmNotARobot It's printing a slice of the list. I have updated the answer to show how you could convert that into a string.
This works great print(' '.join(map(str,grid[x:x+10]))) and actually what i was looking for. Thank you
@IAmNotARobot That will actually print 11 digits if you use +10 btw
1

Try this:

row_length = 10
for row in [grid[i:i + row_length] for i in range(0, len(grid), row_length)]:
    print(row)

Comments

1
grid = [0]*200
print(*[grid[i:i+10] for i in range(0, len(grid), 10)], sep='\n')
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Comments

1

Simple and compact solution, without any external references and exatly your stated output:

for s in [[n for n in grid[i:i+10]] for i in range(0, len(grid), 10)]:
    print(str(s)[1:-1]+',')

Comments

1

try np.matrix

import numpy as np
grid = np.array([0]*100)
B = np.reshape(grid, (-1, len(grid)//10))
print(np.matrix(B))

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

OR

for i in B:
    print(', '.join(map(str, i)))

0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0

Comments

0

Here's an example for a 9-element list, creating sublists of 3 items each:

lst = [0, 0, 0, 0, 0, 0, 0, 0, 0]
largelst = []
smalllst = []

arraysize = 3

counter = 0
for element in lst:
    smalllst.append(element)
    counter += 1
    if counter == arraysize:
        largelst.append(smalllst)
        smalllst = []
        counter = 0
print(largelst)

Output:

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

This works by iterating through the items in lst, and appending them to smalllst. Each iteration of the loop, counter is increased by 1. Every arraysize iterations (in this case, every 3), smalllst is appended to largelst, counter is reset to 0, and smalllst is reset to an empty list.

Comments

0

A simple solution that can handle non-square data.

dataLen = 35
rowLen = 6
data = ["0"] * dataLen

remainder = dataLen % rowLen
n_rows = (dataLen - remainder) / rowLen

start = 0
end = rowLen

for row in range(int(n_rows)):
    print(data[start:end])
    start += rowLen
    end += rowLen

if remainder != 0:
    print(data[-remainder:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.