dictionary.org
author Shantanu <shantanu@fossee.in>
Thu, 29 Apr 2010 17:58:22 +0530
changeset 117 d3005da44457
parent 115 d35eccbf206d
child 119 7dc53e6c8065
permissions -rw-r--r--
Added lists.tex.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
* Lists
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
*** Outline
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
***** Lists
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
***** Tuples
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
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
    In previous tutorial we covered Lists and Tuples and related 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
    functions. In this session we will continue with Python supported
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
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
    we will revisit those concepts and some new ones. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
    We give it a name and it should return a corresponding number. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
    Dictionaries are just key-value pair. For each 'key' there is
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
    corresponding 'value'. In lists we have indexes to access elements,
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
    here we have 'key'. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
    '{}' are used to create Python dictionaries. Lets start by opening 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
    IPython interpreter. Lets create a dictionary say
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}
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
    Its dictionary storing statistics of a cricket player.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
    Now to get the 'average' of this player we have to simply write
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
    print player['Avg']
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
    52.53
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
    To add a new key-value to this dictionary we have to something like
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
    player['Name'] = 'Rahul Dravid'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
    print player    
117
d3005da44457 Added lists.tex.
Shantanu <shantanu@fossee.in>
parents: 115
diff changeset
    33
    Please remember that Python dictionaries dont maintain the order
d3005da44457 Added lists.tex.
Shantanu <shantanu@fossee.in>
parents: 115
diff changeset
    34
    in which the key-value pair are stored it might change as we add new
d3005da44457 Added lists.tex.
Shantanu <shantanu@fossee.in>
parents: 115
diff changeset
    35
    entries.
115
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
    In dictionaries Duplicate keys are overwritten, that is when we do 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
    player['Mat'] = 139
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
    It wont create a new entry, rather it will simply overwrite previous
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
    value with new one. So
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
    print player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
    will have updated value
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
    As we covered in one of previous sessions to iterate through lists 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
    we use 'for'. In case of dictionaries we can iterate over them using 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
    'keys' for example
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
    for key in player:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
        print key, player[key]
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
    We saw how containership works in lists, there we can check if a 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
    value is present in list or not, in case of Dictionaries it works
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
    only for keys. so
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
    'Inn' in player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
    returns True
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
    'Econ' in Player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
    returns False as there is no such 'key'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
    If you try to look or search 'value' it wont work.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
    Dictionaries supports some functions to retrieve keys and values 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
    like
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
    player.keys()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
    returns the list of all 'keys'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
    player.values()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
    return list of all 'values'    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
    Next we shall look at 'sets'. Sets in Python are unordered 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
    collection of unique elements. This data structure comes handy in
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
    situations while removing duplicates from a sequence, and computing 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
    standard math operations on sets such as intersection, union, 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
    difference, and symmetric difference. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
    Lets start by creating a set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
    f10 = set([1,2,3,5,8])
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
    f10 is set of Fibonacci numbers less then 10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
    lets print value of f10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
    print f10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
    As we mentioned earlier, these are unordered structure so order of
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
    elements are not maintained, and output order is different than 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
    input order. Lets create one more set of all prime numbers less than
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
    10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
    p10 = set([2,3,5,7])
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
    print p10.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
    To get union of these two sets we use '|' operator
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
    f10 | p10
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
    For intersection & operator is used:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
    f10 & p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
    f10 - p10 gives difference between f10 and p10, which is, elements
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
    present in f10 but not present in p10.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
    ^ operator gives us symmetric difference that is p10 union f10 minus
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
    f10 intersection p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
    f10 ^ p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
    To check if a set is super set or subset greater than and lesser than
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
    operators can be used
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
    set([2,3]) < p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
    returns True as p10 is superset of given set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
    Similar to lists and dictionaries sets also supports containership so
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
    2 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
    returns True as 2 is part of set p10 and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
    4 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
    returns False.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
    len function works with sets also:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
    len(f10) returns the length, that is 5
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
    We can also use 'for' loops to iterate through a set.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   110
    With this we come to the end of this tutorial on Dictionaries and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
    sets. We have covered some of properties of both data types and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
    functions supported by them. Thank you.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
*** Notes