1

I need to turn my random list of 25 integers into 5 chunks of 5 that go on 5 different lines.

I have found a way to turn the list in to chunks of 5 but I am unsure of how to separate them onto 5 different lines.

import random
def fill(nx, x, y):
    lx = []
    j = 0
    while (j < nx):
        r = random.randint(x, y)
        if r not in lx:
            lx.append(r)
            j = j + 1
    return lx


def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]


def display(lx):
    lx = list(chunks(lx, 5))
    print(lx)

n = 25
a = 10
b = 50
# myList = []
myList = fill(n, a, b)
display(myList)

That is just the code to get the chunks of 5. I know lx.split wouldn't work because it is a list and I can't use \ to split them line by line. So I am unsure of how to get the 5 chunks onto 5 separate lines. Unless I am missing it the difference between mine ( potential duplicate) and the other question is that I have a list of random numbers already generated instead of trying to list out all of the numbers I am turning the 25 I have into chunks separated onto 5 different lines.

Austin S.
  • 41
  • 2
  • concur with @recnac about duplication. also, look into `numpy.reshape()` as an option. details [here](https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.reshape.html) – SanV Apr 13 '19 at 04:13
  • If chunking is not crucial to your question, then [edit] it out. See [MCVE] for more pointers. – wjandrea Apr 13 '19 at 04:15

2 Answers2

1

You could iterate through the list of chunks and print out each one:

for chunk in chunks:
    print(chunk)

The print function automatically inserts a newline character every time it is called.

KYDronePilot
  • 527
  • 3
  • 12
0

a one line option, just for 25 items and 5 lines of 5 chunks:

[print(a[i*5:i*5+5]) for i in range(5)];
Meysam
  • 596
  • 6
  • 12