This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 8 years ago.
if I have a simple for loop like:
for i in range(10):
total = 0
total = total + i
print(total)
How can I print the loops once/together like:
1 2 3 4 5 6 7 8 9
Instead of every loop by itself:
1
2
3 etc
for i in range(10):
total = 0
total = total + i
print(total,end=" ")
The ability of print function, end of the each line changing from \n to " "
Related
This question already has answers here:
Print a list of space-separated elements
(4 answers)
Closed last year.
I have recently started learning Python and wanted to try bubble sort. I'm getting the desired output, but is there a way to not have the space after the final element?
My Output
(0 1 2 4 4 5 7 9 14 18 18 )
The output I want
(0 1 2 4 4 5 7 9 14 18 18)
Ignore the brackets, they are to show the space
def bubble(arr):
n=len(arr)
for i in range(n-1):
for j in range(0,n-i-1):
if arr[j]>arr[j+1]:
arr[j], arr[j+1]= arr[j+1], arr[j]
for k in range(n):
print(arr[k],end=" ")
You can use transform each integer into a string using str(), then use " ".join to avoid having to print the trailing space.
print(" ".join(str(item) for item in arr))
This question already has answers here:
Pascal's Triangle for Python
(12 answers)
Closed 2 years ago.
How do I make a triangle using a recursive function like this:
def triangle(3):
And the triangle should look like this:
1
1 1
1 2 1
And so on.
You can do something like this.
n=4
def triangle(n):
if n==0:
return
num=11**(triangle.n-n)
print "{}{}".format(" "*n, " ".join(list(str(num))))
triangle(n-1)
triangle.n = n
triangle(n)
Output:
1
1 1
1 2 1
1 3 3 1
This question already has answers here:
How to print a string at a fixed width?
(7 answers)
Closed 4 years ago.
So I have code for a 2048 board:
count = 0
for i in range(16):
print(nlist[i], end = ' ')
count += 1
if count == 4:
print("")
count = 0
and this works fine if all the values are single digit numbers:
0 0 0 8
0 4 0 0
0 0 2 2
0 0 0 0
but if I have multiple numbers with more than 1 digit:
16 0 2 2048
8 2 32 64
2 2 0 0
2048 2048 4096 4096
All the spacing gets messed up. Is there any fix for this?
Avoid writing custom functions for doing this. There are lots of python packages out there that can print stuff in a neat table.
My suggestion would be to use PrettyTable
from prettytable import PrettyTable
t = PrettyTable(header=False, border=False)
for i in range(0,16,4):
t.add_row(range(i, i+4))
print t
# 0 1 2 3
# 4 5 6 7
# 8 9 10 11
# 12 13 14 15
As Keatinge mentions in the comment, iterate over the array before printing and find the longest number.
length = max(map(lambda x: len(str(x)), nlist)) + 1
We take nlist, figure out the length of each number when written as text, then take the maximum and add one (the +1 is so that there's a space between the numbers). Then, inside the loop, we stringify the number we're looking at and add spaces as needed.
text = str(x)
text += ' ' * (length - len(text))
Complete example:
count = 0
length = max(map(lambda x: len(str(x)), nlist)) + 1
for i in range(16):
text = str(nlist[i])
text += ' ' * (length - len(text))
print(text, end = '')
count += 1
if count == 4:
print()
count = 0
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 4 years ago.
l = [1,2,3,4,5,6,7,8,9,10]
I have a list and I need to print 5 elements per line to get:
1 2 3 4 5
6 7 8 9 10
I tried everything but I'm stuck, help!
#Joaquin, try below code:
http://rextester.com/BLN7722
l = [1,2,3,4,5,6,7,8,9,10]
for index, item in enumerate(l):
if (index + 1) % 5 == 0:
print(item)
else:
print(item, end=" ")
"""
1 2 3 4 5
6 7 8 9 10
"""
This question already has answers here:
2D array of lists in python
(6 answers)
Closed 6 years ago.
I have a list 2D
a = {}
a[0,0] = 1
a[0,1] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
...
a[1,n] = 6
Now, I want to access the element a[1,x] (x = is 0 to n)
How can I do?
You'll need a nested loop if I understand what your asking for.
somearray = [[0,1],[2,3],[4,5]]
for row in range(len(somearray)):
for col in range(len(somearray[row])):
print(somearray[row][col], end =" ")
will output:
0 1 2 3 4 5