dictionary.org
author Shantanu <shantanu@fossee.in>
Wed, 28 Apr 2010 16:33:18 +0530
changeset 115 d35eccbf206d
child 117 d3005da44457
permissions -rw-r--r--
Added dictionary.org file.
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    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
    Please dont forget that Python dictionaries dont maintain the order
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
    in which key-value pair are stored it changes as we add new entries.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
    In dictionaries Duplicate keys are overwritten, that is when we do 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
    player['Mat'] = 139
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
    It wont create a new entry, rather it will simply overwrite previous
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
    value with new one. So
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
    print player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
    will have updated value
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
    As we covered in one of previous sessions to iterate through lists 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
    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
    45
    'keys' for example
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
    for key in player:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
        print key, player[key]
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
    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
    49
    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
    50
    only for keys. so
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
    'Inn' in player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
    returns True
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
    'Econ' in Player
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
    returns False as there is no such 'key'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
    If you try to look or search 'value' it wont work.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
    Dictionaries supports some functions to retrieve keys and values 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
    like
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
    player.keys()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
    returns the list of all 'keys'
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
    player.values()
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
    return list of all 'values'    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
    Next we shall look at 'sets'. Sets in Python are unordered 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
    collection of unique elements. This data structure comes handy in
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
    situations while removing duplicates from a sequence, and computing 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
    standard math operations on sets such as intersection, union, 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
    difference, and symmetric difference. 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
    Lets start by creating a set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
    f10 = set([1,2,3,5,8])
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
    f10 is set of Fibonacci numbers less then 10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
    lets print value of f10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
    print f10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
    As we mentioned earlier, these are unordered structure so order of
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
    elements are not maintained, and output order is different than 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
    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
    78
    10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
    p10 = set([2,3,5,7])
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
    print p10.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
    To get union of these two sets we use '|' operator
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
    f10 | p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
    For intersection & operator is used:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
    f10 & p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
    f10 - p10 gives difference between f10 and p10, which is, elements
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
    present in f10 but not present in p10.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
    ^ operator gives us symmetric difference that is p10 union f10 minus
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
    f10 intersection p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
    f10 ^ p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
    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
    95
    operators can be used
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
    set([2,3]) < p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
    returns True as p10 is superset of given set
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
    Similar to lists and dictionaries sets also supports containership so
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
    2 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
    returns True as 2 is part of set p10 and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
    4 in p10
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
    returns False.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
    len function works with sets also:
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
    len(f10) returns the length, that is 5
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
    We can also use 'for' loops to iterate through a set.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
    
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
    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
   110
    sets. We have covered some of properties of both data types and 
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
    functions supported by them. Thank you.
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
d35eccbf206d Added dictionary.org file.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
*** Notes