如何通过小数位置对齐数字?

时间:2016-04-11 16:14:13

标签: python python-3.x alignment numeric

我的表:

  2       4,5   100    400
  3        20    10    100
4,2    10.000   500    600
9,2  10.024,5   610  1.100

mylist = [['2', '4,5', '100', '400'], ['3', '20', '10', '100'], ['4,2', '10.000', '500', '600'], ['9,2', '10.024,5', '610', '1.100']]

如何对齐这样的数字:

2         4,5  100    400
3        20     10    100
4,2  10.000    500    600
9,2  10.024,5  610  1.100

我做了什么:

for i in range(0, len(mylist)):
    '{:>10} {:>10} {:>10} {:>10}'.format(mylist[i][0], mylist[i][1], mylist[i][2], mylist[i][3])

这给出了上面的结果,如第1表所示,但小数没有对齐。

更新
我将弗朗西斯科的解决方案改编为任何列表中元素的长度。

mylist = [['1300', '300', '300'], ['2300,5', '300,5', '300,5'], ['3600000000000', '6000000', '6000000'], ['4200,2345', '200,2345', '200,2345'], ['5301', '301', '301'], ['6200', '200', '200'], ['7300', '300', '300000000'], ['8300,5', '300,500000', '300,5'], ['960000', '600', '6000'], ['10200,2345', '200999,2345', '200,2345'], ['11301', '301', '301'], ['12200', '200', '200'], ['13300', '300', '300'], ['14300,5', '300,5', '300,5']]

column2row = zip(*mylist)
maxbefore = []
maxafter = []
for m in column2row:
    maxlengthbefore = 0
    maxlengthafter  = 0
    for e in m:
        if ',' in e:
           lengthbefore = len(str(e).split(",")[0])
           if lengthbefore > maxlengthbefore:
              maxlengthbefore = lengthbefore
           lengthafter  = len(str(e).split(",")[1])
           if lengthafter > maxlengthafter:
              maxlengthafter = lengthafter
        else:
           lengthbefore = len(str(e))
           if lengthbefore > maxlengthbefore:
              maxlengthbefore = lengthbefore
    maxbefore.append(maxlengthbefore)
    maxafter.append(maxlengthafter + 1)

for m in mylist:
    r = 0
    for e in m:
        if ',' in e:
            print(e.rjust(int(maxbefore[r]) + len(e) - e.index(',')).ljust(int(maxbefore[r])+int(maxafter[r])), end=' ')
        else:
            print(e.rjust(int(maxbefore[r])).ljust(int(maxbefore[r])+int(maxafter[r])), end=' ')
        r += 1
    print()

输出

         1300          300              300      
         2300,5        300,5            300,5    
3600000000000      6000000          6000000      
         4200,2345     200,2345         200,2345 
         5301          301              301      
         6200          200              200      
         7300          300        300000000      
         8300,5        300,500000       300,5    
       960000          600             6000      
        10200,2345  200999,2345         200,2345 
        11301          301              301      
        12200          200              200      
        13300          300              300      
        14300,5        300,5            300,5

1 个答案:

答案 0 :(得分:1)

for m in mylist:
    for e in m:
        if ',' in e:
            print(e.rjust(10 + len(e) - e.index(',')).ljust(15), end=' ')
        else:
            print(e.rjust(10).ljust(15), end=' ')
    print()

打印:

         2               4,5           100             400     
         3              20              10             100     
         4,2        10.000             500             600     
         9,2        10.024,5           610           1.100

基本上它的作用是检查字符串中是否有逗号。

如果是这样,它会检查它的位置,将字符串向右居中,检查有多少个字符而不计算逗号后的所有字符,然后将它居中于左边,这次计算逗号后面的位置。

如果字符串中没有逗号,则只将其居中左侧和右侧。