如何按小数点对齐数字?

How to align numbers by decimal position?

我的table:

  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])

以上结果与第一个 table 相同,但小数点未对齐。

更新
我将 Francisco 的解决方案调整为任何列表中元素的长度。

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
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

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

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

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