我怎样才能把这个:打开Python的二维矩阵/列表到表
students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)]
到这一点:
Abe 200
Lindsay 180
Rachel 215
编辑:这应该能够为任何规模大小列表工作。
我怎样才能把这个:打开Python的二维矩阵/列表到表
students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)]
到这一点:
Abe 200
Lindsay 180
Rachel 215
编辑:这应该能够为任何规模大小列表工作。
编辑:有人更改了问题的一个关键细节 阿疣winiचhaudhary给出了一个很好的答案。 /如果你是不是在学习的位置使用的String.Format现在则解决问题的更普遍的/算法的方法是这样的:
for (name, score) in students:
print '%s%s%s\n'%(name,' '*(10-len(name)),score)
>>> students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)]
>>> for a, b in students:
... print '{:<7s} {}'.format(a, b)
...
Abe 200
Lindsay 180
Rachel 215
使用rjust和ljust:
for s in students:
print s[0].ljust(8)+(str(s[1])).ljust(3)
输出:
Abe 200
Lindsay 180
Rachel 215