I'm trying to create a vertical histogram using only built-in modules. I understand the current histogram function isn't truly a histogram (and the code is probably very ugly), but I'm totally lost on how to create a vertical histogram.

import itertools

def histogram(s):
	print("Histogram:")
	print("%s %7s %12s" % ( "No.", "Value", "Histogram" ))
	for num, count in (s):
		print("%3d %7d %s" % (num, count,  "*" * count))

text = "Sample text for this example."
word_list = []
word_seq = []

text = text.strip()

for punc in ".,;:!?'-&[]()" + '"' + '/':
    text = text.replace(punc, "")

words = text.lower().split()

for word in words:
    word_count = len(word)
    word_list.append(word_count)

word_list.sort()

for key, iter in itertools.groupby(word_list):
    word_seq.append((key, len(list(iter))))
    
histogram(word_seq)

Any help is greatly appreciated.

hint: Think about a two dimensional array of asterisks. There is nothing sacred about traversing such an array in row-major order.

Here is my latest with some help I received earlier...

One problem is that I cannot figure why I receive a TypeError no matter how I try to assign tuple_dict. Also, am I even on the right track??? I added posting the length and count before the histogram.

import itertools

def histogram(s):
	tuple_dict = {}
	for i in sorted(s.values()):
		t = ()
		for n in s.keys():
			if s[n] >= i:
				t = t + tuple('*')
			else:
				t = t + tuple(' ')
			tuple_dict[i] = t
	return tuple_dict

text = "This is a sample text for this example to work."
word_list = []
word_seq = []

text = text.strip()

for punc in ".,;:!?'-&[]()" + '"' + '/':
	text = text.replace(punc, "")

words = text.lower().split()

for word in words:
    word_count = len(word)
    word_list.append(word_count)

word_list.sort()

for key, iter in itertools.groupby(word_list):
    word_seq.append((key, len(list(iter))))
    
print("%s %7s" % ( "Length", "Count"))

for num, count in word_seq:
	print("%5d %10d" % (num, count))

d = {}

for a, b in word_seq:
	d.setdefault(a, []).append(b)
	
histogram(d)

You are pretty much going around in circles, it seems to me, so "No, not on the right track (in my opinion)." The problem you have on line 12 is that i is an array holding a single integer. Arrays are mutable, so they cannot be hashed. cast i to a tuple or take its data out of the array and the code is acceptable to Python, but still doesn't do anything useful. (If you post the entire error message, including line number and the full text, it is easier to help... and better yet, if you actually think about the error message "with fresh eyes" (that's the hardest part) you are likely to be able to figure it out for yourself: The people who write these messages are doing their best to be helpful...

Thank you sir. I'm guessing it's obvious that I'm stabbing in the dark here. I'm honestly not trying to ask for the answer, but do you know of any code I could look at to get a better understanding?

The error messages came from me trying to assign tuple_dict as a dict, list or tuple. Each error was line 45 (calling the function) and 12. The line 12 errors were as follows when changing line 4 to each:

tuple_dict = () receives "TypeError: 'tuple' object does not support item assignment"

tuple_dict = [] receives "TypeError: list indices must be integers, not list"

tuple_dict = {} receives "TypeError: unhashable type: 'list'"

tuple_dict = {} receives "TypeError: unhashable type: 'list'"

I would suggest that you print the values and note also that a list can not be used as a dictionary's key. If that is indeed what you want to do, convert to a tuple first.

def histogram(s):
    tuple_dict = {}
    ## don't use "i", "l", or "O" as variable names as they can look like numbers
    for val in sorted(s.values()):
        print type(val), val
        ## comment the rest of the code until you get this problem solved

Also there is a problem with the last line of your code.

Edited 5 Years Ago by woooee: n/a

Consider using the class collections.Counter to count the number of distinct word lengths. Something like

#... get clean word list in words
for word in words:
    counter[len(word)] += 1

Now that you have the data, you need to display it. Think of the display as a two dimensional array. The code you have that works makes that array like this: The first column is lengths of words, and each subsequent column holds an asterisk in the the length row if that row had at least that many words of that length. But what you want this time is exactly the same if you just switch 'column' for 'row' and vice versa. In order to do that, you can either build the array that way, or you can just access the elements in the order you need them.

Edited 5 Years Ago by griswolf: n/a

>>> histogram(([1,56],[2,67]))
Histogram:
No.   Value    Histogram
  1      56 ********************************************************
  2      67 *******************************************************************
>>>

Looks like you have quite good start, you only need to consider the correct scaling by finding the maximum count value before printing.

Here's what I have so far. Unfortunately, it's not producing the desired results. The final output should look like this http://dev.collabshot.com/show/723400/# NOTE: I have changed the text variable here because I am calling a file in the actual version.

import itertools

def histo(dict_words):
	x_max = max(dict_words.keys())
	y_max = max(dict_words.values())
	
	for j in range(400, 0, -20 ):
		print(j, '%3s' % '-|')
		s = '%9s' % '|'
		for i in range(0, x_max):
			if i in dict_words.keys() and dict_words[i] >= j:
				s += '*'
			else:
				s += ' '
		print(s)
		
	for i in range(1, x_max):
		s += '-+-'
	print(s)
	
	for i in range(1, x_max):
		s += ' %d ' % i
	print(s)


text = "This is a sample text for this example to work."
word_list = []
word_seq = []
dicta = {}

text = text.strip()

for punc in ".,;:!?'-&[]()" + '"' + '/':
	text = text.replace(punc, "")

words = text.lower().split()

for word in words:
    word_count = len(word)
    word_list.append(word_count)

word_list.sort()

for key, iter in itertools.groupby(word_list):
    word_seq.append((key, len(list(iter))))
    
print("%s %7s" % ( "Length", "Count"))

for num, count in word_seq:
	print("%5d %10d" % (num, count))

dicta = dict(word_seq)

print(histo(dicta))

I think you are trying to make this too difficult. Format a string to print the line as you want it, and then print the string.

import itertools

def histo(dict_words):
	
    ## length of words from 10 letters to one letter
    for length in range(10, 0, -1 ):
        this_line = '%3d-|%9s' % (length, '-|')  
 
        ## allow for the possibility that the dictionary may 
        ## not contain all lengths from 1-10
        if length in dict_words:
            ## multiply '*' by the number of times this length was found
            this_line += '*' * dict_words[length]

        print this_line

    print '%3d-|       -|----0----1----1----2' % (0)  
    print '              1-3-5----0----5----0'  

text = "This is a sample text for this example to work."
text += 'and the quick brown fox jumped over the lazy, lazydogdog'
word_list = []
word_seq = []
dicta = {}

text = text.strip()

for punc in ".,;:!?'-&[]()" + '"' + '/':
	text = text.replace(punc, "")

words = text.lower().split()

for word in words:
    word_count = len(word)
    word_list.append(word_count)

word_list.sort()

for key, iter in itertools.groupby(word_list):
    word_seq.append((key, len(list(iter))))
    
print("%s %7s" % ( "Length", "Count"))

for num, count in word_seq:
	print("%5d %10d" % (num, count))

dicta = dict(word_seq)

print(histo(dicta))

Edited 5 Years Ago by woooee: n/a

Could someone show me how write the numbers on the left side of the histogram shown here http://dev.collabshot.com/show/723400/#

Here's my code:

import itertools

def histo(dict_words):
	x_max = max(dict_words.keys())
	y_max = max(dict_words.values())
	s = ""
	
	for j in range(y_max, 0, -20 ):
		
		s = '%9s' % '|'
		
		for i in range(1, x_max):
			if i in dict_words.keys() and dict_words[i] >= j:
				s += '***'
			else:
				s += '   '
		print(s)
		
	s = '\t-+'
	for i in range(1, x_max):
		s += '-+-'
	s += '>'
	print(s)

	s = '\t |'
	for i in range(1, x_max):
		if i > 9:
			s += '%d ' % i
		else:
			s += ' %d ' % i 
	print(s)


if __name__ == "__main__":
	f = open('declaration.txt', 'r')
	f.close()
	text = ""
	word_list = []
	word_seq = []
	dicta = {}
	
	open_file = open('declaration.txt', 'r').readlines()
	text = text.join(open_file).strip()
	
	for punc in ".,;:!?'-&[]()" + '"' + '/':
		text = text.replace(punc, "")

	words = text.lower().split()
	
	for word in words:
		word_count = len(word)
		word_list.append(word_count)
	
	word_list.sort()
	
	for key, iter in itertools.groupby(word_list):
		word_seq.append((key, len(list(iter))))
	
	print("%s %7s" % ( "Length", "Count"))
	
	for num, count in word_seq:
		print("%5d %10d" % (num, count))
		
	print(" ")
	
	dicta = dict(word_seq)
	
	histo(dicta)

You could replace line 10 with:

s = ('%9s-|' % (j+1)) if (not (j+1) % 100) else ('%11s' % '|')

However this is not final solution as this depends on y_max which here seems to finish with 99, but could vary according to text. You should debug the code to fix this.

Now the plot does not have connection to file you read. You should debug your code for file IO. It very much wrong, especially lines 35 and 36 which does nothing but cause error if file does not exist. Also iter is build in function and should not be shadowed.

Edited 5 Years Ago by pyTony: n/a

Also your histogram loop does not reach x_max and histogram does not adjust to y_max.

Here is output of my fixed version (leaving your length counting alone, even it is not correct as it joins words together) of your code (. for existing value > 0 at last line)

Length   Count
    1         19
    2        272
    3        279
    4        186
    5        161
    6        145
    7        117
    8         82
    9         67
   10         61
   11         37
   12         15
   13         10
   14          9
   15          2
   16          2
   17          4
   18          2
   19          1
   21          1
 
      300-|                                                               
      290-|                                                               
      280-|                                                               
      270-|   ::::::                                                      
      260-|   ::::::                                                      
      250-|   ::::::                                                      
      240-|   ::::::                                                      
      230-|   ::::::                                                      
      220-|   ::::::                                                      
      210-|   ::::::                                                      
      200-|   ::::::                                                      
      190-|   ::::::                                                      
      180-|   :::::::::                                                   
      170-|   :::::::::                                                   
      160-|   ::::::::::::                                                
      150-|   ::::::::::::                                                
      140-|   :::::::::::::::                                             
      130-|   :::::::::::::::                                             
      120-|   :::::::::::::::                                             
      110-|   ::::::::::::::::::                                          
      100-|   ::::::::::::::::::                                          
       90-|   ::::::::::::::::::                                          
       80-|   :::::::::::::::::::::                                       
       70-|   :::::::::::::::::::::                                       
       60-|   :::::::::::::::::::::::::::                                 
       50-|   :::::::::::::::::::::::::::                                 
       40-|   :::::::::::::::::::::::::::                                 
       30-|   ::::::::::::::::::::::::::::::                              
       20-|   ::::::::::::::::::::::::::::::                              
       10-|:::::::::::::::::::::::::::::::::::::::..................   ...
           -+-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+->
           | 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21

With corrected counts (both your round about method and by defaultdict(int)) I get from attached text this result:

Length   Count
    1         20
    2        272
    3        280
    4        188
    5        161
    6        147
    7        119
    8         89
    9         76
   10         65
   11         36
   12         15
   13         10
   14          7
   15          2

      300-|                                             
      290-|                                             
      280-|      :::                                    
      270-|   ::::::                                    
      260-|   ::::::                                    
      250-|   ::::::                                    
      240-|   ::::::                                    
      230-|   ::::::                                    
      220-|   ::::::                                    
      210-|   ::::::                                    
      200-|   ::::::                                    
      190-|   ::::::                                    
      180-|   :::::::::                                 
      170-|   :::::::::                                 
      160-|   ::::::::::::                              
      150-|   ::::::::::::                              
      140-|   :::::::::::::::                           
      130-|   :::::::::::::::                           
      120-|   :::::::::::::::                           
      110-|   ::::::::::::::::::                        
      100-|   ::::::::::::::::::                        
       90-|   ::::::::::::::::::                        
       80-|   :::::::::::::::::::::                     
       70-|   ::::::::::::::::::::::::                  
       60-|   :::::::::::::::::::::::::::               
       50-|   :::::::::::::::::::::::::::               
       40-|   :::::::::::::::::::::::::::               
       30-|   ::::::::::::::::::::::::::::::            
       20-|:::::::::::::::::::::::::::::::::            
       10-|:::::::::::::::::::::::::::::::::::::::......
           -+-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+->
           | 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

Edited 5 Years Ago by pyTony: n/a

Attachments
THE DECLARATION OF INDEPENDENCE.

In Congress, July 4, 1776.

THE UNANIMOUS DECLARATION OF THE THIRTEEN UNITED STATES OF AMERICA.


July 4, 1776.  When, in the course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the powers of the earth, the separate and equal station to which the laws of nature and of natures God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.

We hold these truths to be self-evident: that all men are created equal; that they are endowed, by their Creator, with certain unalienable rights; that among these are life, liberty, and the pursuit of happiness. That to secure these rights, governments are instituted among men, deriving their just powers from the consent of the governed; that whenever any form of government becomes destructive of these ends, it is the right of the people to alter or to abolish it, and to institute a new government, laying its foundation on such principles, and organizing its powers in such form, as to them shall seem most likely to effect their safety and happiness. Prudence, indeed, will dictate, that governments long established, should not be changed for light and transient causes; and accordingly all experience hath shown, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the same object, evinces a design to reduce them under absolute despotism, it is their right, it is their duty, to throw off such government, and to provide new guards for their future security. Such has been the patient sufferance of these colonies; and such is now the necessity which constrains them to alter their former systems of government. The history of the present King of Great Britain is a history of repeated injuries and usurpations, all having in direct object the establishment of an absolute tyranny over these states. To prove this, let facts be submitted to a candid world.

He has refused his assent to laws the most wholesome and necessary for the public good.

He has forbidden his governors to pass laws of immediate and pressing importance, unless suspended in their operation till his assent should be obtained; and when so suspended, he has utterly neglected to attend to them.

He has refused to pass other laws for the accommodation of large districts of people, unless those people would relinquish the right of representation in the legislature; a right inestimable to them, and formidable to tyrants only. He has called together legislative bodies at places unusual, uncomfortable, and distant from the depository of their public records, for the sole purpose of fatiguing them into compliance with his measures.

He has dissolved representative houses repeatedly, for opposing, with manly firmness, his invasions on the rights of the people. 
 
He has refused for a long time, after such dissolutions, to cause others to be elected; whereby the legislative powers, incapable of annihilation, have returned to the people at large for their exercise; the state remaining, in the mean time, exposed to all the dangers of invasion from without, and convulsions within. 
 
He has endeavored to prevent the population of these States; for that purpose obstructing the laws for naturalization of foreigners; refusing to pass others to encourage their migrations hither, and raising the conditions of new appropriations of lands. 
 
He has obstructed the administration of justice, by refusing his assent to laws for establishing judiciary powers. 
 
He has made judges dependent on his will alone, for the tenure of their offices, and the amount and payment of their salaries. 
 
He has erected a multitude of new offices, and sent hither swarms of officers, to harass our people, and eat out their substance. 
 
He has kept among us, in times of peace, standing armies, without the consent of our legislatures. 
 
He has affected to render the military independent of, and superior to the civil power. 
 
He has combined with others to subject us to a jurisdiction foreign to our constitution, and unacknowledged by our laws; giving his assent to their acts of pretended legislation: 
 
For quartering large bodies of armed troops among us; 
 
For protecting them, by a mock trial, from punishment for any murders which they should commit on the inhabitants of these States; 
 
For cutting off our trade with all parts of the world; 
 
For imposing taxes on us without our consent; 
 
For depriving us, in many cases, of the benefits of trial by jury; 
 
For transporting us beyond seas to be tried for pretended offences; 
 
For abolishing the free system of English laws in a neighbouring province, establishing therein an arbitrary government, and enlarging its boundaries, so as to render it at once an example and fit instrument for introducing the same absolute rule into these colonies; 

For taking away our charters, abolishing our most valuable laws, and altering fundamentally the forms of our governments;

For suspending our own legislatures, and declaring themselves invested with power to legislate for us in all cases whatsoever.

He has abdicated government here, by declaring us out of his protection, and waging war against us.

He has plundered our seas, ravaged our coasts, burnt our towns, and destroyed the lives of our people.

He is at this time transporting large armies of foreign mercenaries to complete the works of death, desolation, and tyranny, already begun with circumstances of cruelty and perfidy, scarcely paralleled in the most barbarous ages, and totally unworthy the head of a civilized nation.

He has constrained our fellow-citizens, taken captive on the high seas, to bear arms against their country, to become the executioners of their friends and brethren, or to fall themselves by their hands.

He has excited domestic insurrections amongst us, and has endeavoured to bring on the inhabitants of our frontiers the merciless Indian savages, whose known rule of warfare is an undistinguished destruction of all ages, sexes, and conditions.

In every stage of these oppressions we have petitioned for redress in the most humble terms. Our repeated petitions have been answered only by repeated injury. A prince, whose character is thus marked by every act which may define a tyrant, is unfit to be the ruler of a free people.

Nor have we been wanting in attentions to our British brethren. We have warned them, from time to time, of attempts by their legislature to extend an unwarrantable jurisdiction over us. We have reminded them of the circumstances of our emigration and settlement here. We have appealed to their native justice and magnanimity, and we have conjured them by the ties of our common kindred to disavow these usurpations, which would inevitably interrupt our connexions and correspondence. They too have been deaf to the voice of justice and of consanguinity. We must, therefore, acquiesce in the necessity which denounces our separation, and hold them, as we hold the rest of mankind, enemies in war, in peace friends.

We, therefore, the representatives of the United States of America, in General Congress assembled, appealing to the Supreme Judge of the world for the rectitude of our intentions, do, in the name, and by authority of the good people of these colonies, solemnly publish and declare, That these United Colonies are, and of right ought to be, Free and Independent States; that they are absolved from all allegiance to the British crown, and that all political connexion between them and the state of Great Britain is, and ought to be, totally dissolved; and that, as Free and Independent States, they have full power to levy war, conclude peace, contract alliances, establish commerce, and to do all other acts and things which Independent States may of right do. And for the support of this Declaration, with a firm reliance on the protection of Divine Providence, we mutually pledge to each other our lives, our fortunes, and our sacred honour.
JOHN HANCOCK.

New Hampshire.Josiah Bartlett, William Whipple, Matthew Thornton.

Massachusetts Bay.Samuel Adams, John Adams, Robert Treat Paine, Elbridge Gerry.

Rhode Island, &c.Stephen Hopkins, William Ellery.

Connecticut.Roger Sherman, Samuel Huntington, William Williams, Oliver Wolcott.

New York.William Floyd, Philip Livingston, Francis Lewis, Lewis Morris.

New Jersey.Richard Stockton, John Witherspoon, Francis Hopkinson, John Hart, Abraham Clark.

Pennsylvania.Robert Morris, Benjamin Rush, Benjamin Franklin, John Morton, George Clymer, James Smith, George Taylor, James Wilson, George Ross.

Delaware.Caesar Rodney, George Read, Thomas McKean.

Maryland.Samuel Chase, William Paca, Thomas Stone, Charles Carroll of Carrollton.

Virginia.George Wythe, Richard Henry Lee, Thomas Jefferson, Benjamin Harrison, Thomas Nelson, Jun., Francis Lightfoot Lee, Carter Braxton.

North Carolina.William Hooper, Joseph Hewes, John Penn.

South Carolina.Edward Rutledge, Thomas Hayward, Jun., Thomas Lynch, Jun., Arthur Middleton.

Georgia.Button Gwinnett, Lyman Hall, George Walton.
This article has been dead for over six months. Start a new discussion instead.