Python Forum
Printing lists in a table, rows and columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing lists in a table, rows and columns
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
Just a sidenote - there is no need to enter list manually. Instead you can:

for num in range(10):
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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()
Reply
#6
try:

numbers = list(range(10))
random.shuffle(numbers)
for num in numbers:
    ...
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a table with different sizes of columns in MS word pepe 8 1,414 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  python script for inserting rows into hbase table lravikumarvsp 7 7,002 Mar-24-2023, 04:44 AM
Last Post: parth_botadara
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,164 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,800 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 752 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,543 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  Dynamically Add rows to table TommyAutomagically 1 2,061 Nov-04-2021, 10:59 PM
Last Post: TommyAutomagically
  making variables in my columns and rows in python kronhamilton 2 1,576 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 2,332 Oct-09-2021, 12:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020