您好我是python的新手,需要使用以下数据打印图表:
每个条的高度是每种单词的计数。 假设我们有三个桶(a,b和c),每个桶中都有许多苹果:
a - 1 b - 3 c - 2因此,如果我们要将其绘制出来,它可能看起来像:
3_x_ 2_xx 1xxx .abc使用'_'作为没有任何数据的单元格的占位符,并使用'x'作为数据的标记。 如何从这些数据中绘制图表?
Hello I am new to python and need to print a graph with the following data:
The height of each bar being the count of each type of word. lets assume we had three buckets (a,b, and c) and in each we had a number of apples:
a - 1 b - 3 c - 2So if we were to chart this out it might look something like:
3_x_ 2_xx 1xxx .abcuse '_' as a place holder for the cell that does not have any data and 'x' as the marker for data. How can I draw a graph from this data?
最满意答案
如果您是Python新手,我强烈建议您浏览他们的库文档: http : //docs.python.org/3/library/ 。 以下是这个问题的一些代码:
def chart(dictionary): height = max(dictionary.values()) # Figures out how tall the diagram needs for i in range(height): # to be, then loops once per level. current_height = height - i # Next line creates a string s = "{0}".format(current_height) # containing the height number. for value in dictionary.values(): # This loop decides for each entry if value >= current_height: # whether to use a 'x' or a '_'. s += 'x' else: s += '_' print(s) s = '.' # These two lines create the lowermost string for key in dictionary.keys(): # with labels. s += key print(s)此代码非常专门针对上述问题,但它应该让您很好地了解您需要去的地方。
If you're new to Python, I highly recommend poking around their library documentation: http://docs.python.org/3/library/. Here's some code for this problem specifically:
def chart(dictionary): height = max(dictionary.values()) # Figures out how tall the diagram needs for i in range(height): # to be, then loops once per level. current_height = height - i # Next line creates a string s = "{0}".format(current_height) # containing the height number. for value in dictionary.values(): # This loop decides for each entry if value >= current_height: # whether to use a 'x' or a '_'. s += 'x' else: s += '_' print(s) s = '.' # These two lines create the lowermost string for key in dictionary.keys(): # with labels. s += key print(s)This code is very specialized for the above problem, but it should give you a good idea of where you need to go.
更多推荐
发布评论