dictionary.org
author asokan <asokan@fossee.in>
Tue, 18 May 2010 15:40:17 +0530
changeset 126 2eac725a5766
parent 121 331c5fdc06d4
permissions -rw-r--r--
changes to array.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
     1
* Dictionaries
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
*** Outline
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
     3
***** Dictionaries
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
     4
***** Sets
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
***** Arsenal Required
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
*** Script
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
    Welcome friends. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
     9
    In previous tutorial we covered Lists, Tuples and related 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    10
    functions. In this session we shall continue with Python
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
    data structures and cover Dictionaries and sets. We have already 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
    covered some basics of Dictionaries in session on Statistics. Here
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    13
    we shall revisit those concepts and some new ones. 
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    15
    We give it a name and it returns a corresponding number. 
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
    Dictionaries are just key-value pair. For each 'key' there is
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    17
    corresponding 'value' associated with it. In lists we use indexes 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    18
    to access elements, here we use the 'key'. 
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    20
    Lets start by opening IPython interpreter. 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    21
    '{}' are used to create Python dictionaries. Lets create a dictionary say
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
    player = {'Mat': 134,'Inn': 233,
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
    'Runs': 10823, 'Avg': 52.53}
121
331c5fdc06d4 Minor edits to dictionaries.org.
Santosh Vattam <vattam@fossee.in>
parents: 119
diff changeset
    25
    Let's see what player contains by typing:
331c5fdc06d4 Minor edits to dictionaries.org.
Santosh Vattam <vattam@fossee.in>
parents: 119
diff changeset
    26
331c5fdc06d4 Minor edits to dictionaries.org.
Santosh Vattam <vattam@fossee.in>
parents: 119
diff changeset
    27
    print player
331c5fdc06d4 Minor edits to dictionaries.org.
Santosh Vattam <vattam@fossee.in>
parents: 119
diff changeset
    28
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    29
    Its a dictionary storing statistics of a cricket player.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    30
    Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    31
    this player we simply type
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
    print player['Avg']
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
    52.53
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    35
    To add a new key-value pair to this dictionary we type
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
    player['Name'] = 'Rahul Dravid'
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    37
    print player
121
331c5fdc06d4 Minor edits to dictionaries.org.
Santosh Vattam <vattam@fossee.in>
parents: 119
diff changeset
    38
    As you can see the given key-value pair has been added.
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    39
    Please note that Python dictionaries don't maintain the order
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    40
    in which the key-value pairs are stored. The order might change
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    41
    as we add new entries.
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
    In dictionaries Duplicate keys are overwritten, that is when we do 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
    player['Mat'] = 139
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
    It wont create a new entry, rather it will simply overwrite previous
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    46
    value with the new one. So
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
    print player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
    will have updated value
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    50
    As we covered in one of previous sessions 'for' can be used to iterate
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    51
    through lists. The same is possible in case of dictionaries too. We can
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    52
    iterate over them using the 'keys', for example:
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
    for key in player:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
        print key, player[key]
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    55
    This prints the keys in the dictionary along with their corresponding 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    56
    values. Notice that the order is not the same as we entered it.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    57
    
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    58
    We saw how containership works in lists. There we can check if a 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    59
    value is present in a list or not but in case of Dictionaries we
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    60
    can only check for the containership of the keys. so
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
    'Inn' in player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
    returns True
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
    'Econ' in Player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
    returns False as there is no such 'key'
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    65
    If you try to look or search for a 'value' it will not work.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    66
    Dictionaries support functions to retrieve keys and values 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    67
    such as
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
    player.keys()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
    returns the list of all 'keys'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
    player.values()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
    return list of all 'values'    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    73
    Now we shall move on to 'sets'. Sets in Python are an unordered 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    74
    collection of unique elements. This data structure comes in handy in
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
    situations while removing duplicates from a sequence, and computing 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
    standard math operations on sets such as intersection, union, 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
    difference, and symmetric difference. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
    Lets start by creating a set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
    f10 = set([1,2,3,5,8])
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    81
    And thats how a set is created.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    82
    f10 is the set of Fibonacci numbers less than 10
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    83
    lets print the value of f10
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
    print f10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
    As we mentioned earlier, these are unordered structure so order of
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    87
    elements is not maintained, and output order is different than 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    88
    input order, just as in dictionaries. Lets create one more set, a set of
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    89
    all prime numbers less than 10
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
    p10 = set([2,3,5,7])
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
    print p10.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    93
    To get union of these two sets we use the or '|' operator
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
    f10 | p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    96
    For intersection we use the and '&' operator:
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
    f10 & p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
    99
    f10 - p10 gives difference between f10 and p10, that is, the set of all elements
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   100
    present in f10 but not in p10.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   101
    The carat '^' operator gives us the symmetric difference of 2 sets. That is
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   102
    f10 union p10 minus f10 intersection p10
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
    f10 ^ p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   105
    To check if a set is the super set or a subset of another set, the greater than 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   106
    and the lesser than operators are used
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
    set([2,3]) < p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
    returns True as p10 is superset of given set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   110
    Similar to lists and dictionaries, sets also supports containership so
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
    2 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
    returns True as 2 is part of set p10 and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
    4 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
    returns False.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   116
    The 'len' function works with sets also:
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   117
    len(f10) returns the length, which is 5 in this case.
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   118
    We can also use 'for' loops to iterate through a set just as with dictionaries and lists.
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   119
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
    With this we come to the end of this tutorial on Dictionaries and 
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   121
    sets. We have seen how to initialize dictionaries, how to index them using keys
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   122
    and a few functions supported by dictionaries. We then saw how to initialize
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   123
    sets, perform various set operations and a few functions supported
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 117
diff changeset
   124
    by sets. Hope you have enjoyed it, Thank you.
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
*** Notes