Python Forum

Full Version: Printing lists in a table, rows and columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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
Just a sidenote - there is no need to enter list manually. Instead you can:

for num in range(10):
(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()
try:

numbers = list(range(10))
random.shuffle(numbers)
for num in numbers:
    ...
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]