Align numbers in a sublist

I have a set of numbers that I want to align with a comma:

   10    3          
  200    4000,222  3  1,5 
  200,21          0,3  2   
30000    4,5      1      

mylist = [['10', '3', '', ''], 
          ['200', '4000,222', '3', '1,5'], 
          ['200,21', '', '0,3', '2'], 
          ['30000', '4,5', '1', '']]

I want to align this list with a comma:

Expected Result:

mylist = [['   10   ', '   3    ', '   ', '   '], 
          ['  200   ', '4000,222', '3  ', '1,5'], 
          ['  200,21', '        ', '0,3', '2  '], 
          ['30000   ', '   4,5  ', '1  ', '   ']]

I tried to rotate the list:

mynewlist = list(zip(*mylist))  

and find the longest decimal part in each sublist:

for m in mynewlist:
    max([x[::-1].find(',') for x in m]

and use rjust and ljust, but I don’t know how to thin out after the decimal point and rjust before the decimal point, as in one line.

How can I resolve this without using format ()? (I want to combine with ljust and rjust)

+4
source share
3 answers

Here is another approach that is currently doing the trick. Unfortunately, I don't see any easy way to do this work, possibly due to time :-)

, . r - , .

r = [[] for i in range(4)]

enumerate:

for ind1, vals in enumerate(zip(*mylist)):

( ):

    l = max(len(v.partition(',')[2]) for v in vals) + 1
    mw = max(len(v if ',' not in v else v.split(',')[0]) for v in vals)

vals (yup, ).

    for ind2, v in enumerate(vals):

, -. , rjust mw, :

        if ',' in v:
            n, d = v.split(',')
            v = "".join((n.rjust(mw),',', d, " " * (l - 1 - len(d))))

.rjust, :

        else:
            v = "".join((v.rjust(mw) + " " * l))

, append r.

        r[ind1].append(v)

:

r = [[] for i in range(4)]
for ind1, vals in enumerate(zip(*mylist)):
    l = max(len(v.partition(',')[2]) for v in vals) + 1
    mw = max(len(v if ',' not in v else v.split(',')[0]) for v in vals)
    for ind2, v in enumerate(vals):
        if ',' in v:
            n, d = v.split(',')
            v = "".join((n.rjust(mw),',', d, " " * (l - 1 - len(d))))
        else:
            v = "".join((v.rjust(mw) + " " * l))
        r[ind1].append(v)

:

>>> print(*map(list,zip(*r)), sep='\n)
['   10   ', '   3    ', '   ', '   ']
['  200   ', '4000,222', '3  ', '1,5']
['  200,21', '        ', '0,3', '2  ']
['30000   ', '   4,5  ', '1  ', '   ']
+2

python 2 3. ljust rjust, , :

mylist = [['10', '3', '', ''], 
          ['200', '4000,222', '3', '1,5'], 
          ['200,21', '', '0,3', '2'], 
          ['30000', '4,5', '1', '']]
transposed = list(zip(*mylist))
sizes = [[(x.index(",") if "," in x else len(x), len(x) - x.index(",") if "," in x else 0)
  for x in l] for l in transposed]
maxima = [(max([x[0] for x in l]), max([x[1] for x in l])) for l in sizes]
withspaces = [
  [' ' * (maxima[i][0] - sizes[i][j][0]) + number + ' ' * (maxima[i][1] - sizes[i][j][1])
  for j, number in enumerate(l)] for i, l in enumerate(transposed)]
result = list(zip(*withspaces))

python3:

>>> print(*result, sep='\n')                             
('   10   ', '   3    ', '   ', '   ')
('  200   ', '4000,222', '3  ', '1,5')
('  200,21', '        ', '0,3', '2  ')
('30000   ', '   4,5  ', '1  ', '   ')
+1

, my_list, . , . , , - , . , '4000,222' (4, 4). , .

from functools import reduce

mylist = [['10', '3', '', ''],
          ['200', '4000,222', '3', '1,5'], 
          ['200,21', '', '0,3', '2'], 
          ['30000', '4,5', '1', '']]

# Return tuple (left part length, right part length) for given string
def part_lengths(s):
    left, sep, right = s.partition(',')
    return len(left), len(sep) + len(right)

# Return string formatted based on part lengths
def format(s, first, second):
    left, sep, right = s.partition(',')
    return left.rjust(first) + sep + right.ljust(second - len(sep))

# Generator yielding part lengths row by row
parts = ((part_lengths(c) for c in r) for r in mylist)

# Combine part lengths to find maximum for each column
# For example data it looks like this: [[5, 3], [4, 4], [1, 2], [1, 2]]
sizes = reduce(lambda x, y: [[max(z) for z in zip(a, b)] for a, b in zip(x, y)], parts)

# Format result based on part lengths
res = [[format(c, *p) for c, p in zip(r, sizes)] for r in mylist]

print(*res, sep='\n')

:

['   10   ', '   3    ', '   ', '   ']
['  200   ', '4000,222', '3  ', '1,5']
['  200,21', '        ', '0,3', '2  ']
['30000   ', '   4,5  ', '1  ', '   ']
+1

Source: https://habr.com/ru/post/1664861/


All Articles