2014-12-06 63 views
-1

我有一个整数我试图均匀地打印出数组,表示该数据集是Python的格式打印到打印均匀数量

7 14 23 49 191

我通过循环这个数组,content

for num in content: 
    print(num,"",end="") 

现在我需要放置一个|以上(通过控制台箱线图代)在我的列表中的某些元素。当某些整数可能占用额外空间时,我怎么知道在哪里放置|符号?我试图

#print | above certain numbers 
for num in content: 
    if num == theone: 
     print("|",end="") 
    else: 
     print(" ",end="") 

#print numbers as before 
for num in content: 
    print(num,"",end="") 

但是这会给我

| | 
7 14 23 49 191 

,而不是

|  | 
7 14 23 49 191 

,因为下面的整数不只是一个数字。有没有一种简单的方法来克服这个问题?由于

回答

3

尝试是这样的:

content = [7, 14, 23, 49, 191,] 
ticks = [0,1,0,1,0] 

fmt = '{:>5}' 

for tic in ticks: 
    print(fmt.format('|' if tic else""), end="") 
print() 
for num in content: 
    print(fmt.format(num) ,end="") 
print() 

编辑:要产生精确格式,根据阿什维尼乔杜里的评论,试试这个方法:

content = [7, 14, 23, 49, 191] 
widths = [' '*len(str(c)) for c in content] 
the_ones = [False, True, False, True, False] # or any other way to 
              # signal which ones to tick 
for i, t in enumerate(the_ones): 
    if t == True: 
     widths[i] = widths[i][:-1]+'|' 

print(' '.join(widths)) 
print(' '.join(map(str, content))) 
+0

这将在开始添加一系列的空间,这就是OP希望不是。 – 2014-12-07 00:19:58

+0

@AshwiniChaudhary请参阅编辑以生成确切的格式。请原谅我不知道,什么是OP? – chapelo 2014-12-07 01:40:24

+0

OP表示原始海报,你的代码现在似乎工作正常。 +1 – 2014-12-07 23:06:30

0

您可以使用一个固定的宽度数字的表示:

# Sample data 
content = [1, 10, 100, 1000] 

# Will be 4, since 1000 has four digits 
max_digits = max(len(str(n)) for n in content) 

# '<4' will be left-aligned; switch to > for right-aligned 
fmt_string = '<{}'.format(max_digits) 

# Print out the digits 
for n in content: 
    s = format(n, fmt_string) 
    # Do your thing to determine whether to place a bar 
    print(s, end='') 

请参阅more关于字符串格式。

+0

请注意,OP将在最后一个数字而不是第一个数字上打印这些管道。 – 2014-12-07 00:20:52

1

你可以做这样的事情:

lst = [7, 14, 23, 49, 191] 

for x in lst: 
    n = len(str(x)) 
    if x in (14, 49): #some condition 
     # if we want to print the | at the end of the number then we first need 
     # to add (n-1) spaces before it and then add a |. Lastly end with a ' ' 
     # so that the cursor moves to the start of next number. 
     print (' ' * (n-1) + '|', end=' ') 
    else: 
     # simply print n space so that cursor moves to the end of the number 
     # and end it with a ' ' to move the cursor to the start of next number 
     print (' ' * n, end=' ') 

print() 

for x in lst: 
    print (x, end=' ') 

输出:

|  |  
7 14 23 49 191 
0
from math import floor,ceil 
numbers = [7, 14, 23, 49, 191] 
selection = [14, 49] 
indexes = sorted([numbers.index(n) for n in selection]) 

def selectors(): 
    string = "" 
    for n in xrange(len(numbers)): 
     len_number = len(str(numbers[n])) 
     if len_number == 1: 
      pad = "|" 
     elif len_number == 2: 
      pad = " |" 
     else: 
      pad = " " * (int(ceil(len_number/2.0)-1)) + "|" + " " * (int(floor(len_number/2.0))) 
     if n not in indexes: 
      pad = pad.replace("|", " ") 
     string += pad+" " 
    return string 

print (selectors()) 
print (" ".join([str(n) for n in numbers])) 

输出:

|  |  
7 14 23 49 191 

这一个是其实很简单,

它会为字符串中的每个字符添加管道(“|”),如果该数字不在所选内容中,则用空格替换管道字符。

其他例子

numbers = [0,100,1000,10000,100000, 1000000, 10000000] 
selection = [1000, 100000, 10000000] 

     |   |    |  
0 100 1000 10000 100000 1000000 10000000