Reputation: 11
I have a list of integer like this
[1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
I want to print maybe 10 int in one line, and then insert a new line so the output should looks like this:
[1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
I need to do this because i need to screenshoot the output of the list to my paper, its better if the list look more tidy. Thanks before.
Upvotes: 0
Views: 626
Reputation: 11
So I was toying around with the yesterday but I had to go to work before I could finished it up. The size variable lets you change the length and it prints it out nicely on the output. If you do need to add brackets to the output that should be easy enough.
Hopefully this points you in the right direction! 👍
myList = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
size = 5 # Number Per Row
startIndex = 0 # Start at the beginning index of myList
try:
for x in range(len(myList)):
for y in range(size):
if startIndex >= len(myList):
break
print(myList[startIndex], end=" ")
startIndex = startIndex + 1
print()
if startIndex >= len(myList):
break
except IndexError:
print("\nIndex Error") # Left this in for debugging purposes
Upvotes: 1
Reputation: 415
I would like to add another answer. It is more pythonic and one-line code.
a, x = list(range(10,77)), 5
print('[', *[str(i)+',' if (j+1)%x!=0 else str(i)+',\n' for j, i in enumerate(a[:-1])],sep='',end=str(a[-1])+']')
Output:
[10,11,12,13,14,
15,16,17,18,19,
20,21,22,23,24,
25,26,27,28,29,
30,31,32,33,34,
35,36,37,38,39,
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
60,61,62,63,64,
65,66,67,68,69,
70,71,72,73,74,
75,76]
Upvotes: 0
Reputation: 415
Try this. I have rigorously tested this one.
n = [1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
i = 0
j = len(n)
j = j - j%10
if len(n)%10==0:
j -= 10
print(end='[')
while i<j:
print(*n[i:i+10], sep=',', end=',\n')
i += 10
print(*n[i:], sep=',', end=']')
Output:
[1,2,1,2,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,2,1,
1,1,1,1,1,1,2,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1]
The example given has 67 elements. My output contains 7 lines. First 6 lines with 10 elements each & the last one with 7 elements. You can replace 10 by any number you want.
Upvotes: 0
Reputation: 331
If you dont want to use any external library, here's one way:
print('['+'\n'.join([','.join(map(str, a[i:i+x])) for i in range(0,len(a),x)])+']')
Upvotes: 0
Reputation: 10624
If you don't want to use a library, here is one way to do it:
for i in range(len(l)//10+1):
if i==0:
k=str(l[i*10:i*10+10])
k=k[:-1]+','
print(k)
elif i==len(l)//10:
k=str(l[i*10:i*10+10])
k=k[1:-1]+']'
print(k)
else:
k=str(l[i*10:i*10+10])
k=k[1:-1]+','
print(k)
Output:
[1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1]
Upvotes: 0
Reputation: 551
Try pprint.
import pprint
foo = [1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
pp = pprint.PrettyPrinter(width=30, compact=True)
pp.pprint(foo)
Results in:
[1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1]
You can play around with the width and indenting, etc...
Upvotes: 0