0

我在调整结果值时遇到了麻烦。

Alist = ["1,25,999",
     "123.4,56.7890,13.571",
     "1,23.45,6,7.8"]

c = 0

while c < len(Alist):
    r = 0
    tokens = Alist[c].split(',')

    while r < len(Alist[c].split(',')):
        if '.' in tokens[r]:
            print "%7.2f" %float(tokens[r]), " ",
        else :
            print "%3d" %float(tokens[r]), "    ",    
        r += 1
        print

    c += 1

我想打印如

    1    25    999
  123.40 56.79  13.57
    1    23.45   6.     7.80

但不知何故它正在打印

    1
   25
  999
   123.40
    56.79
    13.57
     1
    23.45
    6
     7.8

而且我无法弄清楚我的编码有什么问题。

4

3 回答 3

0

print语句不应该在第二个while循环中。只是:

Alist = ["1,25,999",
 "123.4,56.7890,13.571",
 "1,23.45,6,7.8"]

c = 0

while c < len(Alist):
    r = 0
    tokens = Alist[c].split(',')

    while r < len(Alist[c].split(',')):
        if '.' in tokens[r]:
            print "%7.2f" %float(tokens[r]), " ",
        else :
            print "%3d" %float(tokens[r]), "    ",    
        r += 1
    print

    c += 1
于 2013-10-11T01:55:59.477 回答
0

之后r+1,您有一个单独的打印语句。它的缩进级别错误 - 将其向左移动 4 个空格(或一个制表符),它应该可以正常工作。

于 2013-10-11T01:50:28.523 回答
0
In [59]: %paste
myList = ["1,25,999",
     "123.4,56.7890,13.571",
     "1,23.45,6,7.8"]
rows = [r.split(',') for r in myList]
widths = {i:max(len(c) for c in col) for i,col in enumerate(itertools.izip_longest(*rows, fillvalue=""))}
for row in rows:
  for i,val in enumerate(row):
    print " "*((widths[i] - len(val))/2), val, " "*((widths[i] - len(val))/2) if not (widths[i]-len(val))%2 else " "*((widths[i] - len(val)+1)/2),
  print

## -- End pasted text --
   1       25       999   
 123.4   56.7890   13.571 
   1      23.45      6      7.8 
于 2013-10-11T02:13:10.197 回答