如何打印此列表列表?

时间:2019-10-09 17:17:36

标签: python python-3.x list

我应该很好地打印此列表列表,而不用括号,但是我有点挣扎。我不在一行上列出每个列表。我该怎么办?

python
big_numbers = [
        ["Million",    "10^6 ", "10^6 "],
        ["Milliard",   "     ", "10^9 "],
        ["Billion",    "10^9 ", "10^12"],
        ["Billiard",   "     ", "10^15"],
        ["Trillion",   "10^12", "10^18"],
        ["Quadrillion","10^15", "10^24"],
        ["Quintillion","10^18", "10^30"],
        ["Sextillion", "10^21", "10^36"] ]

print('%7s %7s %7s' %('Number', 'long', 'short'))
for i in range(len(big_numbers)):
    for j in range (len(big_numbers[i])):
        print(big_numbers[i][j], end='')

3 个答案:

答案 0 :(得分:2)

此代码对您来说很好用。并会做你想要的。

bigNumbers = [
        ["Million ",    "10^6", "10^6"],
        ["Milliard",   "", "10^9"],
        ["Billion ",    "10^9", "10^12"],
        ["Billiard",   "", "10^15"],
        ["Trillion",   "10^12", "10^18"],
        ["Quadrillion","10^15", "10^24"],
        ["Quintillion","10^18", "10^30"],
        ["Sextillion", "10^21", "10^36"]]

print("{:12} {:6} {:6}".format('Number', 'Long', 'Short'))
for i in bigNumbers:
    print("{:12} {:6} {:6}".format(*i))
  

这是输出的样子

Number           Long        Short
Million          10^6        10^6
Milliard                     10^9
Billion          10^9        10^12
Billiard                     10^15
Trillion         10^12       10^18
Quadrillion      10^15       10^24
Quintillion      10^18       10^30
Sextillion       10^21       10^36

答案 1 :(得分:1)

  • 使用*
  • 解压缩列表
for l in big_numbers:
    print(*l) # you can specify sep='' here for formating.

答案 2 :(得分:1)

解压缩和指定列宽的混合可以使您看起来更漂亮。

for number in big_numbers:
    print('{:12} {:6} {:6}'.format(*number))

结果是:

Million      10^6   10^6 
Milliard            10^9
Billion      10^9   10^12
.....