mardi 21 juin 2016

python 2.7 line.index if statement for a multiplication table

lines = [[str(i * j) for i in xrange(1, 13)] for j in xrange(1, 13)]

for line in lines: for num in line: if line.index(num): print ' ' * (3 - len(num)) + num, else: print ' ' * (2 - len(num)) + num, print

I am trying to understand why the else statement pertains to the first line and the line.index(num) pertains to remaining lines.

Aucun commentaire:

Enregistrer un commentaire