Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python格式打印以均匀打印数字_Python_Python 3.x_Format - Fatal编程技术网

Python格式打印以均匀打印数字

Python格式打印以均匀打印数字,python,python-3.x,format,Python,Python 3.x,Format,我有一个整数数组,我试图均匀地打印出来,比如说数据集是 714239191 我在这个数组中循环,满足于 现在我需要在列表中的某些元素上方放置一个|,以便通过控制台生成方框图。当某些整数可能占用额外空间时,我如何知道将|符号放置在何处?我试过了 #print | above certain numbers for num in content: if num == theone: print("|",end="") else: print(" ",e

我有一个整数数组,我试图均匀地打印出来,比如说数据集是

714239191

我在这个数组中循环,满足于

现在我需要在列表中的某些元素上方放置一个|,以便通过控制台生成方框图。当某些整数可能占用额外空间时,我如何知道将|符号放置在何处?我试过了

#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

因为下面的整数不仅仅是一个数字。有没有一个简单的方法来克服这个问题?谢谢

试试这样的方法:

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()
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=' ')
编辑:根据Ashwini Chaudhary的评论,要生成准确的格式,请尝试以下方法:

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

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

# 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='')

请参见关于字符串格式的说明。

您可以执行以下操作:

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()
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
输出:

   |     |     
7 14 23 49 191
这个其实很简单

将字符串添加到字符串中的每一个字符中,在中间,如果编号不在选择中,则用一个空格替换管字符。

其他例子

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

       |           |               |     
0 100 1000 10000 100000 1000000 10000000

这将在开始时添加一堆空格,而OP并不希望这样做。@AshwiniChaudhary请参见编辑以生成准确的格式。请原谅我不知道,什么是OP?OP是指原始海报,您的代码现在似乎工作正常+1请注意,OP是在最后一位数字而不是第一位数字上打印这些管道。