简体   繁体   English

在不使用 matplotlib 的情况下创建条形图

[英]Create a bar graph without using matplotlib

I need to get a 5 digit integer as an input, and convert this integer into a 2D array and print it out as a bar graph.我需要获取一个 5 位的 integer 作为输入,然后将此 integer 转换为二维数组并将其打印为条形图。 The result I am trying to get is:我试图得到的结果是:

If the input is 19683 , It should return:如果输入是19683 ,它应该返回:

  x
  x   x
  x   x
  x x x
  x x x
  x x x
  x x x x
  x x x x
x x x x x
----------

This is what I have written already这是我已经写的

x = int(input("Enter a 5 digit integer: "))
digits = [int(n) for n in str(x)]
rows = max(digits)
bar_graph = [[0] * len(digits) for i in range(rows)]

But I don't know what I should do from here.但我不知道我应该从这里做什么。

I just need to find a way to replace the 0 s with the x s and in the right order.我只需要找到一种方法以正确的顺序将0替换为x

You started off good.你开局不错。 You need to have each digit and to know the maximum digit (you got with rows = max(digits) ).你需要知道每个数字并知道最大数字(你得到了rows = max(digits) )。

Now all you need to do is loop the rows in decreasing order, and for each digit check if it needs to be marked in this row.现在您需要做的就是按降序循环这些行,并为每个数字检查它是否需要在该行中标记。 This will be true when the digit is greater-than or equal-to the row number:当数字大于或等于行号时,这将是正确的:

x = int(input("Enter a 5 digit integer: "))
digits = [int(n) for n in str(x)]
rows = max(digits)
bar_graph = []
for row in range(rows, 0, -1):
    bar_graph.append(['x' if digit >= row else ' ' for digit in digits])
for row in bar_graph:
    print(' '.join(row))
print('-'*(2*len(digits)+3))

But note that it's not really necessary to store everything in a 2D list and you can print directly by iterating the rows and digits:但请注意,实际上没有必要将所有内容存储在 2D 列表中,您可以通过迭代行和数字直接打印:

for row in range(rows, 0, -1):
    print(row, ':', ' '.join(['x' if digit >= row else ' ' for digit in digits]))
print('-'*(2*len(digits)+3))

Will give:会给:

Enter a 5 digit integer: 19683
9 :   x      
8 :   x   x  
7 :   x   x  
6 :   x x x  
5 :   x x x  
4 :   x x x  
3 :   x x x x
2 :   x x x x
1 : x x x x x
-------------

Note that this will always "truncate" the graph to the largest digit, for example:请注意,这将始终将图形“截断”为最大数字,例如:

Enter a 5 digit integer: 11132
3 :       x  
2 :       x x
1 : x x x x x
-------------

If you want the graph to always be "complete" (starting with 9), just change this line:如果您希望图表始终“完整”(从 9 开始),只需更改此行:

rows = 9

To get:要得到:

Enter a 5 digit integer: 11132
9 :          
8 :          
7 :          
6 :          
5 :          
4 :          
3 :       x  
2 :       x x
1 : x x x x x
-------------

try this:尝试这个:

num = input()
print("\n".join([" ".join([" " if 10-line>int(j) else "x" for j in num]) for line in range(1,10)]))

Here you go:这里是 go:

number = input("Enter a number: ")
ls = [9-int(x) for x in s]
for i in range(9,0,-1):
    print(i, ':', *[' ' if v>0 else '*' for v in ls])
    ls = [x-1 for x in ls]
print('-'*(len(s)*2+4))

# input: 19683
# Output:
9 :   *      
8 :   *   *  
7 :   *   *  
6 :   * * *  
5 :   * * *  
4 :   * * *  
3 :   * * * *
2 :   * * * *
1 : * * * * *
-------------

Come on, and I'll put in my five cents.来吧,我会投入我的五分钱。

x = 196594345345345432

digits = [int(n) for n in str(x)]

for i in reversed(range(1, max(digits) + 1)):
    this_row = ''
    for digit in digits:
        if digit >= i:
            this_row += 'x'
        else:
            this_row += ' '
    print(this_row)

print('-'.rjust(len(digits), '-'))
 x  x             
 x  x             
 x  x             
 xx x             
 xxxx   x  x  x   
 xxxxx xx xx xxx  
 xxxxxxxxxxxxxxxx 
 xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM