Last active
June 23, 2017 19:30
-
-
Save mGalarnyk/224c8dd6597e6908dcf01f43fc338d29 to your computer and use it in GitHub Desktop.
Python Basics 11: Word Count (Filter out Punctuation, Dictionary Manipulation, and Sorting Lists) tutorial for the blog post: https://medium.com/@GalarnykMichael/python-basics-10-dictionaries-and-dictionary-methods-4e9efa70f5b9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h1 align=\"center\">Task: Word Count </h1>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Count the number of times each word occurs\n", | |
"<br>\n", | |
"\n", | |
"For the text below, \n", | |
"<br>\n", | |
"a) Clean punctuation and transform all words to lower case.\n", | |
"<br>\n", | |
"b) Output a list of word count pairs sorted from highest to lowest count." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"Text=\"\"\"\n", | |
"bands which have connected them with another, and to assume among the\n", | |
"powers of the earth, the separate and equal station to which the Laws\n", | |
"of Nature and of Nature's God entitle them, a decent respect to the\n", | |
"opinions of mankind requires that they should declare the causes which\n", | |
"impel them to the separation. We hold these truths to be\n", | |
"self-evident, that all men are created equal, that they are endowed by\n", | |
"their Creator with certain unalienable Rights, that among these are\n", | |
"Life, Liberty and the pursuit of Happiness.--That to secure these\n", | |
"rights, Governments are instituted among Men, deriving their just\n", | |
"powers from the consent of the governed, --That whenever any Form of\n", | |
"Government becomes destructive of these ends, it is the Right of the\n", | |
"People to alter or to abolish it, and to institute new Government,\n", | |
"laying its foundation on such principles and organizing its powers in\n", | |
"such form, as to them shall seem most likely to effect their Safety\n", | |
"and Happiness. \"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Cleaning text and lower casing all words\n", | |
"for char in '-.,\\n':\n", | |
" Text=Text.replace(char,' ')\n", | |
"Text = Text.lower()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 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 nature's 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 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 \n" | |
] | |
} | |
], | |
"source": [ | |
"print(Text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['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', \"nature's\", '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', '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']\n" | |
] | |
} | |
], | |
"source": [ | |
"# split returns a list of words delimited by sequences of whitespace (including tabs, newlines, etc, like re's \\s) \n", | |
"word_list = Text.split()\n", | |
"print(word_list)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# 1. Using for loops\n", | |
"\n", | |
"# Initializing Dictionary\n", | |
"d = {}\n", | |
"\n", | |
"# counting number of times each word comes up in list of words (in dictionary)\n", | |
"for word in word_list: \n", | |
" d[word] = d.get(word, 0) + 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'a': 1,\n", | |
" 'abolish': 1,\n", | |
" 'all': 1,\n", | |
" 'alter': 1,\n", | |
" 'among': 3,\n", | |
" 'and': 7,\n", | |
" 'another': 1,\n", | |
" 'any': 1,\n", | |
" 'are': 4,\n", | |
" 'as': 1,\n", | |
" 'assume': 1,\n", | |
" 'bands': 1,\n", | |
" 'be': 1,\n", | |
" 'becomes': 1,\n", | |
" 'by': 1,\n", | |
" 'causes': 1,\n", | |
" 'certain': 1,\n", | |
" 'connected': 1,\n", | |
" 'consent': 1,\n", | |
" 'created': 1,\n", | |
" 'creator': 1,\n", | |
" 'decent': 1,\n", | |
" 'declare': 1,\n", | |
" 'deriving': 1,\n", | |
" 'destructive': 1,\n", | |
" 'earth': 1,\n", | |
" 'effect': 1,\n", | |
" 'endowed': 1,\n", | |
" 'ends': 1,\n", | |
" 'entitle': 1,\n", | |
" 'equal': 2,\n", | |
" 'evident': 1,\n", | |
" 'form': 2,\n", | |
" 'foundation': 1,\n", | |
" 'from': 1,\n", | |
" 'god': 1,\n", | |
" 'governed': 1,\n", | |
" 'government': 2,\n", | |
" 'governments': 1,\n", | |
" 'happiness': 2,\n", | |
" 'have': 1,\n", | |
" 'hold': 1,\n", | |
" 'impel': 1,\n", | |
" 'in': 1,\n", | |
" 'institute': 1,\n", | |
" 'instituted': 1,\n", | |
" 'is': 1,\n", | |
" 'it': 2,\n", | |
" 'its': 2,\n", | |
" 'just': 1,\n", | |
" 'laws': 1,\n", | |
" 'laying': 1,\n", | |
" 'liberty': 1,\n", | |
" 'life': 1,\n", | |
" 'likely': 1,\n", | |
" 'mankind': 1,\n", | |
" 'men': 2,\n", | |
" 'most': 1,\n", | |
" 'nature': 1,\n", | |
" \"nature's\": 1,\n", | |
" 'new': 1,\n", | |
" 'of': 9,\n", | |
" 'on': 1,\n", | |
" 'opinions': 1,\n", | |
" 'or': 1,\n", | |
" 'organizing': 1,\n", | |
" 'people': 1,\n", | |
" 'powers': 3,\n", | |
" 'principles': 1,\n", | |
" 'pursuit': 1,\n", | |
" 'requires': 1,\n", | |
" 'respect': 1,\n", | |
" 'right': 1,\n", | |
" 'rights': 2,\n", | |
" 'safety': 1,\n", | |
" 'secure': 1,\n", | |
" 'seem': 1,\n", | |
" 'self': 1,\n", | |
" 'separate': 1,\n", | |
" 'separation': 1,\n", | |
" 'shall': 1,\n", | |
" 'should': 1,\n", | |
" 'station': 1,\n", | |
" 'such': 2,\n", | |
" 'that': 6,\n", | |
" 'the': 12,\n", | |
" 'their': 3,\n", | |
" 'them': 4,\n", | |
" 'these': 4,\n", | |
" 'they': 2,\n", | |
" 'to': 11,\n", | |
" 'truths': 1,\n", | |
" 'unalienable': 1,\n", | |
" 'we': 1,\n", | |
" 'whenever': 1,\n", | |
" 'which': 3,\n", | |
" 'with': 2}" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1, 'opinions'),\n", | |
" (1, 'all'),\n", | |
" (1, 'just'),\n", | |
" (1, 'consent'),\n", | |
" (1, 'earth'),\n", | |
" (2, 'its'),\n", | |
" (1, 'causes'),\n", | |
" (1, 'should'),\n", | |
" (11, 'to'),\n", | |
" (1, 'alter'),\n", | |
" (4, 'them'),\n", | |
" (2, 'government'),\n", | |
" (1, 'bands'),\n", | |
" (2, 'they'),\n", | |
" (1, 'governments'),\n", | |
" (1, 'laying'),\n", | |
" (1, 'right'),\n", | |
" (1, 'people'),\n", | |
" (1, 'truths'),\n", | |
" (1, 'ends'),\n", | |
" (1, 'likely'),\n", | |
" (4, 'are'),\n", | |
" (1, \"nature's\"),\n", | |
" (1, 'organizing'),\n", | |
" (1, 'principles'),\n", | |
" (1, 'god'),\n", | |
" (1, 'shall'),\n", | |
" (1, 'liberty'),\n", | |
" (1, 'unalienable'),\n", | |
" (1, 'safety'),\n", | |
" (2, 'happiness'),\n", | |
" (1, 'new'),\n", | |
" (1, 'foundation'),\n", | |
" (1, 'we'),\n", | |
" (1, 'nature'),\n", | |
" (2, 'men'),\n", | |
" (1, 'pursuit'),\n", | |
" (1, 'separation'),\n", | |
" (1, 'by'),\n", | |
" (1, 'on'),\n", | |
" (1, 'created'),\n", | |
" (1, 'institute'),\n", | |
" (9, 'of'),\n", | |
" (2, 'equal'),\n", | |
" (1, 'or'),\n", | |
" (3, 'among'),\n", | |
" (1, 'secure'),\n", | |
" (1, 'another'),\n", | |
" (1, 'respect'),\n", | |
" (1, 'from'),\n", | |
" (1, 'decent'),\n", | |
" (1, 'creator'),\n", | |
" (3, 'their'),\n", | |
" (1, 'station'),\n", | |
" (1, 'entitle'),\n", | |
" (1, 'certain'),\n", | |
" (1, 'life'),\n", | |
" (2, 'form'),\n", | |
" (6, 'that'),\n", | |
" (1, 'becomes'),\n", | |
" (1, 'instituted'),\n", | |
" (1, 'be'),\n", | |
" (1, 'hold'),\n", | |
" (2, 'with'),\n", | |
" (1, 'evident'),\n", | |
" (2, 'rights'),\n", | |
" (4, 'these'),\n", | |
" (1, 'impel'),\n", | |
" (1, 'assume'),\n", | |
" (3, 'powers'),\n", | |
" (1, 'declare'),\n", | |
" (7, 'and'),\n", | |
" (1, 'endowed'),\n", | |
" (1, 'is'),\n", | |
" (2, 'it'),\n", | |
" (1, 'as'),\n", | |
" (1, 'have'),\n", | |
" (1, 'in'),\n", | |
" (1, 'seem'),\n", | |
" (1, 'any'),\n", | |
" (1, 'self'),\n", | |
" (1, 'abolish'),\n", | |
" (3, 'which'),\n", | |
" (1, 'separate'),\n", | |
" (1, 'effect'),\n", | |
" (1, 'deriving'),\n", | |
" (1, 'most'),\n", | |
" (1, 'connected'),\n", | |
" (1, 'mankind'),\n", | |
" (2, 'such'),\n", | |
" (1, 'destructive'),\n", | |
" (1, 'a'),\n", | |
" (1, 'whenever'),\n", | |
" (1, 'governed'),\n", | |
" (12, 'the'),\n", | |
" (1, 'requires'),\n", | |
" (1, 'laws')]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Reverses the key and values so they can be sorted\n", | |
"# using tuples\n", | |
"word_freq = []\n", | |
"for key, value in d.items():\n", | |
" word_freq.append((value, key))\n", | |
"word_freq" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(12, 'the'), (11, 'to'), (9, 'of'), (7, 'and'), (6, 'that'), (4, 'these'), (4, 'them'), (4, 'are'), (3, 'which'), (3, 'their'), (3, 'powers'), (3, 'among'), (2, 'with'), (2, 'they'), (2, 'such'), (2, 'rights'), (2, 'men'), (2, 'its'), (2, 'it'), (2, 'happiness'), (2, 'government'), (2, 'form'), (2, 'equal'), (1, 'whenever'), (1, 'we'), (1, 'unalienable'), (1, 'truths'), (1, 'station'), (1, 'should'), (1, 'shall'), (1, 'separation'), (1, 'separate'), (1, 'self'), (1, 'seem'), (1, 'secure'), (1, 'safety'), (1, 'right'), (1, 'respect'), (1, 'requires'), (1, 'pursuit'), (1, 'principles'), (1, 'people'), (1, 'organizing'), (1, 'or'), (1, 'opinions'), (1, 'on'), (1, 'new'), (1, \"nature's\"), (1, 'nature'), (1, 'most'), (1, 'mankind'), (1, 'likely'), (1, 'life'), (1, 'liberty'), (1, 'laying'), (1, 'laws'), (1, 'just'), (1, 'is'), (1, 'instituted'), (1, 'institute'), (1, 'in'), (1, 'impel'), (1, 'hold'), (1, 'have'), (1, 'governments'), (1, 'governed'), (1, 'god'), (1, 'from'), (1, 'foundation'), (1, 'evident'), (1, 'entitle'), (1, 'ends'), (1, 'endowed'), (1, 'effect'), (1, 'earth'), (1, 'destructive'), (1, 'deriving'), (1, 'declare'), (1, 'decent'), (1, 'creator'), (1, 'created'), (1, 'consent'), (1, 'connected'), (1, 'certain'), (1, 'causes'), (1, 'by'), (1, 'becomes'), (1, 'be'), (1, 'bands'), (1, 'assume'), (1, 'as'), (1, 'any'), (1, 'another'), (1, 'alter'), (1, 'all'), (1, 'abolish'), (1, 'a')]\n" | |
] | |
} | |
], | |
"source": [ | |
"word_freq.sort(reverse=True)\n", | |
"print(word_freq)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# 2. Not using Get Method\n", | |
"\n", | |
"# Initializing Dictionary\n", | |
"d = {}\n", | |
"\n", | |
"# counting number of times each word comes up in list of words (in dictionary)\n", | |
"for word in word_list:\n", | |
" if word not in d:\n", | |
" d[word] = 0\n", | |
" d[word] += 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'a': 1,\n", | |
" 'abolish': 1,\n", | |
" 'all': 1,\n", | |
" 'alter': 1,\n", | |
" 'among': 3,\n", | |
" 'and': 7,\n", | |
" 'another': 1,\n", | |
" 'any': 1,\n", | |
" 'are': 4,\n", | |
" 'as': 1,\n", | |
" 'assume': 1,\n", | |
" 'bands': 1,\n", | |
" 'be': 1,\n", | |
" 'becomes': 1,\n", | |
" 'by': 1,\n", | |
" 'causes': 1,\n", | |
" 'certain': 1,\n", | |
" 'connected': 1,\n", | |
" 'consent': 1,\n", | |
" 'created': 1,\n", | |
" 'creator': 1,\n", | |
" 'decent': 1,\n", | |
" 'declare': 1,\n", | |
" 'deriving': 1,\n", | |
" 'destructive': 1,\n", | |
" 'earth': 1,\n", | |
" 'effect': 1,\n", | |
" 'endowed': 1,\n", | |
" 'ends': 1,\n", | |
" 'entitle': 1,\n", | |
" 'equal': 2,\n", | |
" 'evident': 1,\n", | |
" 'form': 2,\n", | |
" 'foundation': 1,\n", | |
" 'from': 1,\n", | |
" 'god': 1,\n", | |
" 'governed': 1,\n", | |
" 'government': 2,\n", | |
" 'governments': 1,\n", | |
" 'happiness': 2,\n", | |
" 'have': 1,\n", | |
" 'hold': 1,\n", | |
" 'impel': 1,\n", | |
" 'in': 1,\n", | |
" 'institute': 1,\n", | |
" 'instituted': 1,\n", | |
" 'is': 1,\n", | |
" 'it': 2,\n", | |
" 'its': 2,\n", | |
" 'just': 1,\n", | |
" 'laws': 1,\n", | |
" 'laying': 1,\n", | |
" 'liberty': 1,\n", | |
" 'life': 1,\n", | |
" 'likely': 1,\n", | |
" 'mankind': 1,\n", | |
" 'men': 2,\n", | |
" 'most': 1,\n", | |
" 'nature': 1,\n", | |
" \"nature's\": 1,\n", | |
" 'new': 1,\n", | |
" 'of': 9,\n", | |
" 'on': 1,\n", | |
" 'opinions': 1,\n", | |
" 'or': 1,\n", | |
" 'organizing': 1,\n", | |
" 'people': 1,\n", | |
" 'powers': 3,\n", | |
" 'principles': 1,\n", | |
" 'pursuit': 1,\n", | |
" 'requires': 1,\n", | |
" 'respect': 1,\n", | |
" 'right': 1,\n", | |
" 'rights': 2,\n", | |
" 'safety': 1,\n", | |
" 'secure': 1,\n", | |
" 'seem': 1,\n", | |
" 'self': 1,\n", | |
" 'separate': 1,\n", | |
" 'separation': 1,\n", | |
" 'shall': 1,\n", | |
" 'should': 1,\n", | |
" 'station': 1,\n", | |
" 'such': 2,\n", | |
" 'that': 6,\n", | |
" 'the': 12,\n", | |
" 'their': 3,\n", | |
" 'them': 4,\n", | |
" 'these': 4,\n", | |
" 'they': 2,\n", | |
" 'to': 11,\n", | |
" 'truths': 1,\n", | |
" 'unalienable': 1,\n", | |
" 'we': 1,\n", | |
" 'whenever': 1,\n", | |
" 'which': 3,\n", | |
" 'with': 2}" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(12, 'the'), (11, 'to'), (9, 'of'), (7, 'and'), (6, 'that'), (4, 'these'), (4, 'them'), (4, 'are'), (3, 'which'), (3, 'their'), (3, 'powers'), (3, 'among'), (2, 'with'), (2, 'they'), (2, 'such'), (2, 'rights'), (2, 'men'), (2, 'its'), (2, 'it'), (2, 'happiness'), (2, 'government'), (2, 'form'), (2, 'equal'), (1, 'whenever'), (1, 'we'), (1, 'unalienable'), (1, 'truths'), (1, 'station'), (1, 'should'), (1, 'shall'), (1, 'separation'), (1, 'separate'), (1, 'self'), (1, 'seem'), (1, 'secure'), (1, 'safety'), (1, 'right'), (1, 'respect'), (1, 'requires'), (1, 'pursuit'), (1, 'principles'), (1, 'people'), (1, 'organizing'), (1, 'or'), (1, 'opinions'), (1, 'on'), (1, 'new'), (1, \"nature's\"), (1, 'nature'), (1, 'most'), (1, 'mankind'), (1, 'likely'), (1, 'life'), (1, 'liberty'), (1, 'laying'), (1, 'laws'), (1, 'just'), (1, 'is'), (1, 'instituted'), (1, 'institute'), (1, 'in'), (1, 'impel'), (1, 'hold'), (1, 'have'), (1, 'governments'), (1, 'governed'), (1, 'god'), (1, 'from'), (1, 'foundation'), (1, 'evident'), (1, 'entitle'), (1, 'ends'), (1, 'endowed'), (1, 'effect'), (1, 'earth'), (1, 'destructive'), (1, 'deriving'), (1, 'declare'), (1, 'decent'), (1, 'creator'), (1, 'created'), (1, 'consent'), (1, 'connected'), (1, 'certain'), (1, 'causes'), (1, 'by'), (1, 'becomes'), (1, 'be'), (1, 'bands'), (1, 'assume'), (1, 'as'), (1, 'any'), (1, 'another'), (1, 'alter'), (1, 'all'), (1, 'abolish'), (1, 'a')]\n" | |
] | |
} | |
], | |
"source": [ | |
"# this next bit of code reverses the key and values so they can be sorted\n", | |
"# using tuples\n", | |
"word_freq = []\n", | |
"for key, value in d.items():\n", | |
" word_freq.append((value, key))\n", | |
"word_freq.sort(reverse=True)\n", | |
"print word_freq" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[('the', 12),\n", | |
" ('to', 11),\n", | |
" ('of', 9),\n", | |
" ('and', 7),\n", | |
" ('that', 6),\n", | |
" ('them', 4),\n", | |
" ('are', 4),\n", | |
" ('these', 4),\n", | |
" ('among', 3),\n", | |
" ('their', 3),\n", | |
" ('powers', 3),\n", | |
" ('which', 3),\n", | |
" ('its', 2),\n", | |
" ('government', 2),\n", | |
" ('they', 2),\n", | |
" ('happiness', 2),\n", | |
" ('men', 2),\n", | |
" ('equal', 2),\n", | |
" ('form', 2),\n", | |
" ('with', 2),\n", | |
" ('rights', 2),\n", | |
" ('it', 2),\n", | |
" ('such', 2),\n", | |
" ('opinions', 1),\n", | |
" ('all', 1),\n", | |
" ('just', 1),\n", | |
" ('consent', 1),\n", | |
" ('earth', 1),\n", | |
" ('causes', 1),\n", | |
" ('should', 1),\n", | |
" ('alter', 1),\n", | |
" ('bands', 1),\n", | |
" ('governments', 1),\n", | |
" ('laying', 1),\n", | |
" ('right', 1),\n", | |
" ('people', 1),\n", | |
" ('truths', 1),\n", | |
" ('ends', 1),\n", | |
" ('likely', 1),\n", | |
" (\"nature's\", 1),\n", | |
" ('organizing', 1),\n", | |
" ('principles', 1),\n", | |
" ('god', 1),\n", | |
" ('shall', 1),\n", | |
" ('liberty', 1),\n", | |
" ('unalienable', 1),\n", | |
" ('safety', 1),\n", | |
" ('new', 1),\n", | |
" ('foundation', 1),\n", | |
" ('we', 1),\n", | |
" ('nature', 1),\n", | |
" ('pursuit', 1),\n", | |
" ('separation', 1),\n", | |
" ('by', 1),\n", | |
" ('on', 1),\n", | |
" ('created', 1),\n", | |
" ('institute', 1),\n", | |
" ('or', 1),\n", | |
" ('secure', 1),\n", | |
" ('another', 1),\n", | |
" ('respect', 1),\n", | |
" ('from', 1),\n", | |
" ('decent', 1),\n", | |
" ('creator', 1),\n", | |
" ('station', 1),\n", | |
" ('entitle', 1),\n", | |
" ('certain', 1),\n", | |
" ('life', 1),\n", | |
" ('becomes', 1),\n", | |
" ('instituted', 1),\n", | |
" ('be', 1),\n", | |
" ('hold', 1),\n", | |
" ('evident', 1),\n", | |
" ('impel', 1),\n", | |
" ('assume', 1),\n", | |
" ('declare', 1),\n", | |
" ('endowed', 1),\n", | |
" ('is', 1),\n", | |
" ('as', 1),\n", | |
" ('have', 1),\n", | |
" ('in', 1),\n", | |
" ('seem', 1),\n", | |
" ('any', 1),\n", | |
" ('self', 1),\n", | |
" ('abolish', 1),\n", | |
" ('separate', 1),\n", | |
" ('effect', 1),\n", | |
" ('deriving', 1),\n", | |
" ('most', 1),\n", | |
" ('connected', 1),\n", | |
" ('mankind', 1),\n", | |
" ('destructive', 1),\n", | |
" ('a', 1),\n", | |
" ('whenever', 1),\n", | |
" ('governed', 1),\n", | |
" ('requires', 1),\n", | |
" ('laws', 1)]" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# 3. Solution using inbuilt methods\n", | |
"# Probably not practical to expect for new programmers to Python\n", | |
"\n", | |
"# initializing a dictionary\n", | |
"d = {};\n", | |
"\n", | |
"# counting number of times each word comes up in list of words\n", | |
"for key in word_list: \n", | |
" d[key] = d.get(key, 0) + 1\n", | |
"\n", | |
"sorted(d.items(), key = lambda x: x[1], reverse = True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.youtube.com/watch?v=l_dIleafLZ8" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment