day1/session3.tex
changeset 166 ddfd95133adc
parent 161 ff22fae4fde5
child 167 5f13be28532d
equal deleted inserted replaced
163:f7aeeedb9aa3 166:ddfd95133adc
   288     fields = record.split(';')
   288     fields = record.split(';')
   289   \end{lstlisting}
   289   \end{lstlisting}
   290 \end{frame}
   290 \end{frame}
   291 
   291 
   292 \begin{frame}[fragile]
   292 \begin{frame}[fragile]
   293   \frametitle{Dictionary}
       
   294   \begin{itemize}
       
   295     \item lists index: 0 \ldots n
       
   296     \item dictionaries index using any hashable objects
       
   297     \item d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}
       
   298     \item d[``Terminator''] => ``I'll be back''
       
   299     \item ``Terminator'' is called the key of \typ{d}
       
   300     \item ``I'll be back'' is called the value of the key ``Terminator''
       
   301   \end{itemize}
       
   302 \end{frame}
       
   303 
       
   304 \begin{frame}[fragile]
       
   305   \frametitle{Dictionary - Building parsed data}
   293   \frametitle{Dictionary - Building parsed data}
   306   \begin{itemize}
   294   \begin{itemize}
   307     \item Let the parsed data be stored in dictionary \typ{data}
   295     \item Let the parsed data be stored in dictionary \typ{data}
   308     \item Keys of \typ{data} are strings - region codes
   296     \item \begin{lstlisting}
   309     \item Value of the key is another dictionary.
   297 data = {}  # is an empty dictionary
       
   298 \end{lstlisting}
       
   299     \item Index of a dictionary is called a \emph{key}
       
   300     \item \emph{Keys} of \typ{data} are strings - region codes
       
   301     \item Value of a \emph{key} can be any Python object
       
   302   \end{itemize}
       
   303 \end{frame}
       
   304 
       
   305 \begin{frame}[fragile]
       
   306   \frametitle{Dictionary - Building parsed data...}
       
   307   \begin{itemize}
       
   308     \item In this problem let the value of a \emph{key} be another dictionary.
   310     \item This dictionary contains:
   309     \item This dictionary contains:
   311     \begin{itemize}
   310     \begin{itemize}
   312       \item 'marks': A list of NumPy arrays
   311       \item 'marks': A \emph{list} of NumPy arrays
   313       \item 'total': Total marks of each student
   312       \item 'total': Total marks of each student
   314       \item 'P': Number of passes
   313       \item 'P': Number of passes
   315       \item 'F': Number of failures
   314       \item 'F': Number of failures
   316       \item 'W': Number of withdrawls
   315       \item 'W': Number of withdrawls
   317     \end{itemize}
   316     \end{itemize}
   325 data = {}
   324 data = {}
   326 for record in open('sslc1.txt'):
   325 for record in open('sslc1.txt'):
   327     fields = record.split(';')
   326     fields = record.split(';')
   328     if fields[0] not in data:
   327     if fields[0] not in data:
   329         data[fields[0]] = {
   328         data[fields[0]] = {
   330             'marks': array([]),
   329             'marks': [],
   331             'total': array([]),
   330             'total': [],
   332             'P': 0,
   331             'P': 0,
   333             'F': 0,
   332             'F': 0,
   334             'W': 0
   333             'W': 0
   335             }
   334             }
   336   \end{lstlisting}
   335   \end{lstlisting}
   396     data[k]['sub_avg'] = sub_avg
   395     data[k]['sub_avg'] = sub_avg
   397     data[k]['sub_std'] = sub_std
   396     data[k]['sub_std'] = sub_std
   398   \end{lstlisting}
   397   \end{lstlisting}
   399 \end{frame}
   398 \end{frame}
   400 
   399 
       
   400 \begin{frame}[fragile]
       
   401   \frametitle{New Concepts}
       
   402   \begin{itemize}
       
   403    \item Dictionaries
       
   404    \item Slicing lists
       
   405    \item New type of conditional
       
   406    \item NumPy arrays
       
   407    \item Slicing NumPy arrays
       
   408    \item NumPy array operations - square, average, sqrt
       
   409   \end{itemize}
       
   410 \end{frame}
       
   411 
   401 \end{document}
   412 \end{document}
   402