a - 1 b - 3 c - 2

3_x_ 2_xx 1xxx .abc

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

So if we were to chart this out it might look something like:

3_x_ 2_xx 1xxx .abc

use '_' 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?

## 最满意答案

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.