Page 377 - Computer Science Class 11 With Functions
P. 377

●  myDict.setdefault(key, [value]): setdefault() method returns the value of the item with the
              specified key. However, if the key does not exist, then the key is inserted in the dictionary and the key is associated
              with the value passed as the second parameter.
             Example:
             >>> myDict = { 'b':'beta', 'g':'gamma', 'a':'alpha' }
             >>> myDict.setdefault('a')
                 'alpha'
             >>> myDict.setdefault('a', 'aa')
                 'alpha'
             >>> myDict
                 {'b': 'beta', 'g': 'gamma', 'a': 'alpha'}
             >>> myDict.setdefault('d','delta')
                 'delta'
             >>> myDict
                 {'b': 'beta', 'g': 'gamma', 'a': 'alpha', 'd': 'delta'}

            14.6 Sorting Keys/ Values of a Dictionary

            sorted(): 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']
            Example: 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
            14.7 Building a Thesaurus

            In this section, we will build a thesaurus. For this purpose, we write a program that creates and manages a thesaurus
            in the form of a dictionary. More specifically, the program should do the following:
            •  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')}
            •  opulate the thesaurus with the word: synonyms pairs. For each word, synonyms is a tuple of its synonyms.
              P
              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.
            •  When no more word: synonyms are to be added, display  the thesaurus constructed in step b.
            •  epeatedly ask the user, whether he/she would like to continue with the thesaurus operations. So long as the user
              R
              responds with Y or y, do the following:

               i. Ask the user to enter a word  (w) whose synonyms are required.



                                                                                                   Dictionaries  375
   372   373   374   375   376   377   378   379   380   381   382