Page 215 - Information_Practice_Fliipbook_Class11
P. 215

Sample Output:

                 apple 3
                 banana 2
                 orange 1
            In each of the above examples, we use a for loop to iterate over the keys, values, or items of the dictionary. The
            for loop assigns each key, value, or key-value pair to a variable (depending on which method we're using) during
            each iteration, and the loop body then executes some code using that variable. Note that the order in which the
            keys, values, or items are traversed is not guaranteed to be the same as the order in which they were added to
            the dictionary.

            8.6 Sorting Keys/ Values of a Dictionary

            The method sorted() is used to sort the dictionary by the keys or by the values. By default, the method
            sorted() returns a list of keys in the dictionary in ascending order of the keys.
            Example:

             >>> myDict = { 'b':'beta', 'g':'gamma', 'a':'alpha' }
             >>> sorted(myDict)  # sorted(myDict.keys())
                 ['a', 'b', 'g']
             >>> sorted(myDict, reverse = True)
                 ['g', 'b', 'a']
             >>> sorted(myDict.values())
                 ['alpha', 'beta', 'gamma']
            Let us now use sorted() method to print all key-value pairs with respect to keys.

             >>> for key in sorted(myDict.keys()):
             ...      print(key, ':', myDict[key])
                 a : alpha
                 b : beta
                 g : gamma
            8.7 Building a Thesaurus

            In this section, we will build a thesaurus. For this purpose, we write a program (Program 8.1 ) that creates and manages
            a thesaurus in the form of a dictionary. More specifically, the program should do the following:
            a.  Begin with a thesaurus of a few words, such as the following thesaurus:
             >>> thesaurus = {'beautiful':('alluring', 'delightful', 'charming', 'pleasing', 'attractive'),\
                                     'lovely':('cute', 'dazzling'), \
                                     'pretty': ('nice-looking', 'beautiful')}
            b.   Populate the thesaurus with the word: synonyms pairs. For each word, synonyms is a tuple of its synonyms.

                The program should repeatedly prompt the user with a message Want to add more words and synonyms?
                (Y/y) and continue to accept word: synonyms so long as  the user responds with Y or y.
            c.  When no more word: synonyms are to be added, display  the thesaurus constructed in step b.

            d.   Repeatedly ask the user, whether he/she would like to continue with the thesaurus operations. So long as the user
                responds with Y or y, do the following:
                i.  Ask the user to enter a word  (w) whose synonyms are required.
                ii.  Prompt the user to enter w with the message 'Enter a word:'
                iii.  Find the synonyms of the word w (entered by the user) in  thesaurus.

                iv.  The program should yield all synonyms of w.
                   A word s is considered the synonym of w, if
                     either s is in the tuple corresponding to the key w in thesaurus, that is,  s is in thesaurus[w]



                                                                                             Python Dictionaries  201
   210   211   212   213   214   215   216   217   218   219   220