我怎樣才能把這個:打開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