-1

I have a list (A) that is the product of ( x * y). I am trying to create y lists that contain x elements.

I need to either print a new line every x elements, or divide the list into y lists that contain x elements.

I've researched many similar threads, but still have no idea.

Thank you

edit: to address some of the recent comments @ b4hand, thank you for letting me know. I have removed that. @ jonrsharpe, this is part of a larger assignment. I need to create a specific shape with input. I have managed to do the rest, and I believe that splitting it like I described will result in the shape I want. I haven't really tried anything, but I've read the following threads: - How do you split a list into evenly sized chunks? - http://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/ - Splitting a list of into N parts of approximately equal length

  • Going over the links I think I found one resource that I need to look into. I'm not really looking to get code, I'm just looking for a tutorial or related tips to get started.

edit: I was unsuccessful in trying to use pprint to resolve my issue.

ediit: The values of x and y will be entered by the user.

edit: @joergwork that is close, but I do not want to be counting by 3

Community
  • 1
  • 1
JohnKraz
  • 109
  • 1
  • 8
  • In python, importing modules is a normal practice. Even the standard library is only accessible if you import it. With this restriction, you're likely going to get answers that only reinvent the wheel instead of a more Pythonic answer that reuses existing functionality. – b4hand Oct 07 '14 at 20:05
  • So what have you tried, and what is the problem with it? Which "threads" have you read; what did they teach you, and what do you still not understand? – jonrsharpe Oct 07 '14 at 20:09
  • Thank you for your replies, I have addressed them in the OP. – JohnKraz Oct 07 '14 at 20:18
  • Do you already know the values of x and/or y? – Malnormalulo Oct 07 '14 at 20:29
  • The values of x and y will be entering by the user. – JohnKraz Oct 07 '14 at 20:36
  • put some input and ur expected output. – James Oct 07 '14 at 21:24

2 Answers2

0

Are you looking for something like the following (from a python console)?

>>> a = [1,2,3,4,5,6,7,8,9]
>>> x = 3
>>> [ a[1*i::x] for i in range(x) ]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>>
>>>
>>> for i in range(x):
...     a[1*i::x]
...
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

edit: like so?

>>> for i in range(x):
...     a[x*i:x*i+x]
...
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

edit: made a working example out of yours: like that?

x = int(input("Please enter a x: "))
y = int(input("Please enter a y: "))

product = x * y

numbers = []
for i in range(1, product + 1):
    numbers.append(i)

num_cols, num_rows = x, y

for row in range(num_rows):
    print('row {}: values: {}'.format(row, numbers[num_cols * row:num_cols * row + num_cols]))
joergwork
  • 141
  • 6
  • Edit: That is very close to it, but I'd like it to look like [1,2,3] [3,4,5] [6,7,9]. I do not want to be skipping three numbers between each element. – JohnKraz Oct 07 '14 at 20:56
  • So I managed to implement that structure into my code, but I'm still having issues. I'm going to look at this a bit more, then reply. Thanks – JohnKraz Oct 07 '14 at 21:36
  • @JohnKraz you might have to do something like range(x+1) if the array is not divisable by x. – joergwork Oct 07 '14 at 21:36
  • Thanks again for your reply. I have noticed two issues. I am going to reply with my code, so it is more clear – JohnKraz Oct 07 '14 at 21:40
  • @JohnKraz, I hope you understand it. And it would be nice to upvote this answer and/or mark it as answer. – joergwork Oct 07 '14 at 23:41
0
#ask for x

z = int(input("Please enter a x: "))

#ask for y
y = int(input("Please enter a y: "))

product = x * y


alist = []
for i in range(1,product+1):
    nums.append(i)

a = alist

x = z 

for i in range(x):
    print(a[x*i:x*i+x])

When I run this code I seem to get (x - y) number of "[]" underneath the code.

JohnKraz
  • 109
  • 1
  • 8