sets.rst
author Puneeth Chaganti <punchagan@fossee.in>
Thu, 07 Oct 2010 12:28:12 +0530
changeset 242 a33e942379d7
parent 143 e75538bca178
child 229 5c647a197103
permissions -rw-r--r--
Cleaned up script for getting started with files LO.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     2
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     4
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     6
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     7
In this tutorial, we shall learn
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     8
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     9
 * sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    10
 * operations on sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    11
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    12
Sets are data structures which contain unique elements. In other words,
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    13
duplicates are not allowed in sets.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    14
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    15
Lets look at how to input sets.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    16
type
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    17
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    18
 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    19
    a_list = [1, 2, 1, 4, 5, 6, 7]
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    20
    a = set(a_list)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    21
    a
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    22
     
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    23
We can see that duplicates are removed and the set contains only unique
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    24
elements. 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    25
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    26
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    27
    f10 = set([1, 2, 3, 5, 8])
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    28
    p10 = set([2, 3, 5, 7])
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    29
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    30
f10 is the set of fibonacci numbers from 1 to 10.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    31
p10 is the set of prime numbers from 1 to 10.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    32
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    33
Various operations that we do on sets are possible here also.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    34
The | character stands for union
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    35
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    36
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    37
    f10 | p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    38
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    39
gives us the union of f10 and p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    40
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    41
The & character stands for intersection.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    42
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    43
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    44
    f10 & p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    45
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    46
gives the intersection
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    47
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    48
similarly,
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    49
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    50
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    51
    f10 - p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    52
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    53
gives all the elements that are in f10 but not in p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    54
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    55
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    56
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    57
    f10 ^ p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    58
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    59
is all the elements in f10 union p10 but not in f10 intersection p10. In
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    60
mathematical terms, it gives the symmectric difference.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    61
143
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    62
Sets also support checking of subsets.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    63
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    64
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    65
    b = set([1, 2])
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    66
    b < f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    67
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    68
gives a True since b is a proper subset of f10.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    69
Similarly,
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    70
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    71
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    72
    f10 < f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    73
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    74
gives a False since f10 is not a proper subset.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    75
hence the right way to do would be
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    76
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    77
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    78
    f10 <= f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    79
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    80
and we get a True since every set is a subset of itself.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    81
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    82
Sets can be iterated upon just like lists and tuples. 
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    83
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    84
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    85
    for i in f10:
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    86
        print i,
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    87
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    88
prints the elements of f10.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    89
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    90
The length and containership check on sets is similar as in lists and tuples.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    91
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    92
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    93
    len(f10)
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    94
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    95
shows 5. And
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    96
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    97
    
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    98
    1 in f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    99
    2 in f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   100
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   101
prints True and False respectively
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   102
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   103
The order in which elements are organised in a set is not to be relied upon 
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   104
since sets do not support indexing. Hence, slicing and striding are not valid
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   105
on sets.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   106
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   107
{{{ Pause here and try out the following exercises }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   108
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   109
%% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   110
        list all the duplicates
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   111
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   112
{{{ continue from paused state }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   113
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   114
Duplicates marks are the marks left out when we remove each element of the 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   115
list exactly one time.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   116
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   117
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   118
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   119
    marks = [20, 23, 22, 23, 20, 21, 23] 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   120
    marks_set = set(marks)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   121
    for mark in marks_set:
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   122
        marks.remove(mark)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   123
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   124
    # we are now left with only duplicates in the list marks
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   125
    duplicates = set(marks)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   126
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   127
{{{ Show summary slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   128
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   129
This brings us to the end of the tutorial.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   130
we have learnt
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   131
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   132
 * How to make sets from lists
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   133
 * How to input sets
143
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   134
 * How to perform union, intersection and symmectric difference operations
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   135
 * How to check if a set is a subset of other
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   136
 * The various similarities with lists like length and containership
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   137
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   138
{{{ Show the "sponsored by FOSSEE" slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   139
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   140
#[Nishanth]: Will add this line after all of us fix on one.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   141
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   143
Hope you have enjoyed and found it useful.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   144
Thankyou
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   145
 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   146
.. Author              : Nishanth
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   147
   Internal Reviewer 1 : 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   148
   Internal Reviewer 2 : 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   149
   External Reviewer   :