Python Forum
Printing lists in a table, rows and columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Printing lists in a table, rows and columns (/thread-12621.html)



Printing lists in a table, rows and columns - randy_shooflay - Sep-03-2018

I'm using python3.7.
In this first example it runs without errors.
My goal is to print the numbers in one row and the letters on the second row (or visa versa) aligned in columns. Zero over "a", one over "b", etc.
I would like help in understanding the python logic so I can achieve my goal.
numbers = [0,1,2,3,4,5,6,7,8,9]
letters = ["a","b","c","d","e","f","g","h","i","j"]

for row in range(2):
    for col in range(10):
        for num in numbers:
            print(num, end='')
        fir let in letters:
            print(let, end='')
    print()
Output:
0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij 0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij
This next example does not run, but does error.
numbers = [0,1,2,3,4,5,6,7,8,9]
letters = ["a","b","c","d","e","f","g","h","i","j"]

for row in range(2):
    for col in range(10):
        for num in numbers:
            if row==0 and col==0:
                print(num, end='')
            else:
                print(letters, end='')
    print()
Error:
tut1.py, line 16 else: ^ TabError: inconsistent use of tabs and spaces in indentation



RE: Printing lists in a table, rows and columns - Axel_Erfurt - Sep-03-2018

numbers = [0,1,2,3,4,5,6,7,8,9]
letters = ["a","b","c","d","e","f","g","h","i","j"]
 
for num in numbers:
    print(num, end='\t')
print("\r")
for let in letters:
    print(let, end='\t')
Output:
0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j



RE: Printing lists in a table, rows and columns - randy_shooflay - Sep-04-2018

axel_Erfurt, I believe I understand the issue better from your example.
To illustrate:
numbers = [0,1,2,3,4,5,6,7,8,9]
letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
	
for num in numbers:
    print(num, end=' ')
print()
for let in letters[0:10]:
    print(let, end=' ')
print()
for let in letters[10:]:
    print(let, end=' ')
print()
Output:
0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t
pythontutor.com/live-edit-mode


RE: Printing lists in a table, rows and columns - perfringo - Sep-04-2018

Just a sidenote - there is no need to enter list manually. Instead you can:

for num in range(10):



RE: Printing lists in a table, rows and columns - randy_shooflay - Sep-05-2018

(Sep-04-2018, 06:13 AM)perfringo Wrote: Just a sidenote - there is no need to enter list manually. Instead you can:

for num in range(10):

This is problematic though, for instance where can I put 'random.shuffle(num)'?
If I place random.shuffle(num) before the for loop the variable 'num' has not been created yet.
If I place random.shuffle(num) after the for loop the variable 'num' has already executed printing to screen.
If I place random.shuffle(num) within the for loop the for loop errors on execution.


import random

numbers = [0,1,2,3,4,5,6,7,8,9]
letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]

for num in range(10):
	random.shuffle(num)
	print(num, end=' ')
print()
     
for num in numbers:
    print(num, end=' ')
print()
for let in letters[0:10]:
    print(let, end=' ')
print()
for let in letters[10:]:
    print(let, end=' ')
print()
Error:
Traceback (most recent call last): File "py-forum4.py", line 7, in <module> random.shuffle(num) File "/usr/local/lib/python3.7/random.py", line 275, in shuffle for i in reversed(range(1, len(x))): TypeError: object of type 'int' has no len()



RE: Printing lists in a table, rows and columns - ichabod801 - Sep-05-2018

try:

numbers = list(range(10))
random.shuffle(numbers)
for num in numbers:
    ...



RE: Printing lists in a table, rows and columns - perfringo - Sep-05-2018

In previous posts there was no random.shuffle to be seen.

My suggestion was based on this piece of code:

numbers = [0,1,2,3,4,5,6,7,8,9]
...
for num in numbers:
    print(num, end='\t')
Which can be expressed:

for num in range(10):
    print(num, end='\t')
Regarding random.shuffle: best way is to start with built-in help:

>>> import random
>>> help(random.shuffle)
Help on method shuffle in module random:

shuffle(x, random=None) method of random.Random instance
    Shuffle list x in place, and return None.
    
    Optional argument random is a 0-argument function returning a
    random float in [0.0, 1.0); if it is the default None, the
    standard random.random will be used.
(END)
Your code will not work, it doesn't matter whether you use for num in numbers or for num in range(10), argument must be a list.

>>> import random
>>> r = list(range(10))
>>> random.shuffle(r)
>>> r
[1, 8, 2, 5, 4, 9, 0, 6, 7, 3]
>>> random.shuffle(r)
>>> r
[0, 6, 5, 1, 4, 7, 9, 2, 3, 8]