We'll need additional setup here.
=====<font color="navy">Using the word "love"</font>=====
* We will use the play <b> Romeo and Juliet </b>
Make a python program, call it <code> RomeoJuliet.py</code> save it under the same directory as where you've saved the Shakespearean texts.
2. In your program, open the file "Romeo and Juliet.txt" (The text file need to be saved in the same directory as your python program to be able to use local file name)
3. Check to see that the file was opened properly
4. Now create a variable that represents the string "love", for example:
>>> lv_counter = 0;
6.Use a for loop (or while loop, if you like) to read through the lines of the file. While you are reading each line, count the number of lines that contains the word "love"
7. Does Shakespeare use a lot of love in his plays? How about other synonyms of "love"?
* Recall that lists in Python can contain <b>
arbitrary objects</b> and <b>dynamically expand</b> as new things "arrive"
* We may declare our list as such
* Dictionaries are another data structure commonly used in programming. Dictionaries
(think of dictionary in real life) stores what we call <b> key-value pairs</b>. One common thing to do with dictionary is determine an entry's value given its key.
* Entries in the dictionary are stored <b>
orderless </b> (i.e. no way to arrange storage position according to value)
* <b>keys</b> for each entry in the dictionary must be unique, <b> values </b> do not have to be unique.
* For our simplicity, we will use a string as keys.
* The following basic operations
works with entries in a dictionary (remove an entry, add an entry, check if a key is in the dictionary, print the value corresponding to a given key):
>>> del myDict["Interesting"]
For more information on dictionary: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
====<font color="navy">Lists and Dictionaries Exercises====
<b>List & Iteration Exercise 1:</b>
* Create a new python
* Import the list of characters from <b> A Midsummer Night's Dream </b>
saved in the file <code> AMsND_Char.txt</code>
* Save it to a list, where each item in the list should be the name of the character enclosed in angled brackets, e.g. ['<HELENA>', ...]
* Print the list to see that you got it right
* For good practice, you should close the file by using
>>> myFile = open("name_of_file", "r")
<b>List & Iteration Exercise 2:</b>
* In the same program that you created from exercise 1
Now open the play <b> A Midsummer Night's Dream </b>
Read through the play line by line, count how many times has <b> <OBERON> </b> spoke.
* Print to screen the name <OBERON> and the number of times he spoke.
* Close the file.
<b>List & Iteration Exercise 3*:</b>
* Use the same program as above
* Open the same play again
* Now iterate through the list of character you saved from exercise 1, see how many times each of them speak.
* Print to screen each character's name and the number of times they spoke.
* Close the file.
* <b>Note:</b> You can only readline() through a file once, so for each line read, you must check that if any of the characters have started a new speech that line.
<b>List & Iteration Exercise 4**:</b>
* Do the same as exercise 3, except now instead of printing them directly, save the <b>names of character</b> and <b>number of times they spoke</b> as a <b>key:value</b> pair in to a dictionary.
* Print the dictionary to screen and check that you have the same result as part 3.
Who speaks the most in Shakespeare's A Midsummer Night's Dream? Can you guess (or if you know) the pairings of the couples in the story?▼
* Who speaks the most in Shakespeare's A Midsummer Night's Dream? Can you guess (or if you know) the pairings of the couples in the story?