Cleaned up manipulating lists.
authorPuneeth Chaganti <punchagan@fossee.in>
Tue, 09 Nov 2010 16:07:33 +0530
changeset 421 c4c5d1123f07
parent 413 da1f270b07b7
child 422 f29d9014e1fc
Cleaned up manipulating lists.
manipulating-lists/script.rst
manipulating-lists/slides.org
manipulating-lists/slides.tex
--- a/manipulating-lists/script.rst	Tue Nov 09 15:33:47 2010 +0530
+++ b/manipulating-lists/script.rst	Tue Nov 09 16:07:33 2010 +0530
@@ -22,13 +22,12 @@
 
 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
 
-
 {{{ Show the slide containing the outline }}}
 
 We have already learnt a lot about Lists in Python. In this tutorial,
 we will learn more about advanced features of Lists in Python. We will
-see in detail how to concatenate two lists, slicing and striding of
-lists, methods to sort and reverse the list.
+see how to concatenate two lists, details of slicing and striding of
+lists, methods to sort and reverse lists.
 
 {{{ Shift to terminal and start ipython }}}
 
@@ -40,8 +39,8 @@
 
 We already know what Lists are in Python, how to access individual
 elements in the list and some of the functions that can be run on the
-lists like max, min, sum len and so on. Now let us learn some of the
-basic operations that can be performed on Lists.
+lists like ``max, min, sum, len`` and so on. Now let us learn some of
+the basic operations that can be performed on Lists.
 
 We already know how to access individual elements in a List. But what
 if we have a scenario where we need to get a part of the entire list
@@ -64,6 +63,18 @@
 was the element with the index 4 was included but 23 which was the
 element with index 8 was excluded.
 
+Following is an exercise you must do. 
+
+%% %% Obtain the primes less than 10, from the list ``primes``. 
+
+Please, pause the video here, do the exercise and then resume. 
+
+::
+
+  primes[0:4]
+
+will give us the primes below 10. 
+
 Generalizing, we can obtain a slice of the list "p" from the index
 "start" upto the index "end" but excluding "end" with the following
 syntax
@@ -79,12 +90,12 @@
   num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
 
 If we want to obtain all the odd numbers less than 10 from the list
-"num" we have to start from element with index 1 upto the index 10 in
+``num`` we have to start from element with index 1 upto the index 10 in
 steps of 2::
 
   num[1:10:2]
 
-So if we don't specify the step it is by default 1. Similary there are
+When no step is specified, it is assumed to be 1. Similarly, there are
 default values for start and stop indices as well. If we don't specify
 the start index it is implicitly taken as the first element of the
 list::
@@ -105,6 +116,19 @@
 
 gives us all the even numbers in the list "num".
 
+Following is an exercise that you must do. 
+
+%% %% Obtain all the multiples of three from the list ``num``.
+
+Please, pause the video here. Do the exercise and then continue. 
+
+::
+
+  num[::3]
+
+gives us all the multiples of 3 from the list, since every third
+element in it, starting from 0, is divisible by 3. 
+
 The other basic operation that we can perform on list is concatenation
 of two or more lists. We can combine two lists by using the "plus"
 operator. Say we have
@@ -122,17 +146,16 @@
   c
 
 It is important to observe that the "plus" operator always returns a
-new list without touching anything in the existing lists which are the
-operands of the concatenation operation.
+new list without altering the lists being concatenated in any way. 
 
-We know that list is a collection of data. Whenever we have a
-collection we run into situations where we want to start the
+We know that a list is a collection of data. Whenever we have a
+collection we run into situations where we want to sort the
 collection. Lists support sort method which sorts the list inplace::
 
   a = [5, 1, 6, 7, 7, 10]
   a.sort()
 
-Now the contents of the list "a" will be::
+Now the contents of the list ``a`` will be::
 
   a
   [1, 5, 6, 7, 7, 10]
@@ -164,27 +187,46 @@
   a
   [5, 4, 3, 2, 1]
 
-But again the original list is lost. If we want to obtain the reverse
-of a list keeping the original list intact we can use the Python
-built-in function reversed. reversed function returns a new list which
-is the reverse of the list which was passed as the argument to the
-reversed function::
+But again the original list is lost. 
+.. #[punch: removed reversed, since it returns an iterator]
 
-  a = [1, 2, 3, 4, 5]
-  reversed(a)
+To reverse a list, we could use striding with negative indexing.::
+
+   a[::-1]
 
 We can also store this new reversed list in another list variable.
 
+Following is an (are) exercise(s) that you must do. 
+
+%% %% Given a list of marks of students in an examination, obtain a
+      list with marks in descending order.
+      ::
+
+            marks = [99, 67, 47, 100, 50, 75, 62]
+
+Please, pause the video here. Do the exercise(s) and then continue. 
+
+::
+
+  sorted(marks)[::-1]
+
+OR
+
+::
+
+  sorted(marks, reverse = True)
+
+
+
 {{{ Show summary slide }}}
 
 This brings us to the end of another session. In this tutorial session
 we learnt
 
-  * How to define strings
-  * Different types of defining a string
-  * String concatenation and repeatition
-  * Accessing individual elements of the string
-  * Immutability of strings
+  * Obtaining parts of lists using slicing and striding
+  * List concatenation
+  * Sorting lists 
+  * Reversing lists
 
 {{{ Show the "sponsored by FOSSEE" slide }}}
 
--- a/manipulating-lists/slides.org	Tue Nov 09 15:33:47 2010 +0530
+++ b/manipulating-lists/slides.org	Tue Nov 09 16:07:33 2010 +0530
@@ -18,7 +18,7 @@
 #+LaTeX_HEADER:  commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
 #+LaTeX_HEADER:  showstringspaces=false, keywordstyle=\color{blue}\bfseries}
 
-#+TITLE:    Accessing parts of arrays
+#+TITLE:    Manipulating Lists
 #+AUTHOR:    FOSSEE
 #+EMAIL:     
 #+DATE:    
@@ -28,83 +28,54 @@
 #+LANGUAGE:  en
 #+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
 #+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc
+#+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate
 
 * Outline
-  - Manipulating one and multi dimensional arrays
-  - Access and change individual elements 
-  - Access and change rows and columns 
-  - Slicing and striding on arrays to access chunks 
-  - Read images into arrays and manipulations
-* Sample Arrays
-  #+begin_src python
-    In []: A = array([12, 23, 34, 45, 56])
-    
-    In []: C = array([[11, 12, 13, 14, 15],
-                      [21, 22, 23, 24, 25],
-                      [31, 32, 33, 34, 35],
-                      [41, 42, 43, 44, 45],
-                      [51, 52, 53, 54, 55]])
-    
-  #+end_src
+  In this session we shall be looking at 
+  - Concatenating lists
+  - Obtaining parts of lists
+  - Sorting lists
+  - Reversing lists
 * Question 1
-  Change the last column of ~C~ to zeroes. 
+  Obtain the primes less than 10, from the list ~primes~. 
 * Solution 1
   #+begin_src python
-    In []:  C[:, -1] = 0
-  #+end_src
+    primes[0:4]
+  #+end_src python
+* Slicing
+  #+begin_src python
+    p[start:stop]
+  #+end_src python
+  - Returns all elements of ~p~ between ~start~ and ~stop~
+  - The element with index equal to ~stop~ is *not* included. 
 * Question 2
-  Change ~A~ to ~[11, 12, 13, 14, 15]~. 
+  Obtain all the multiples of three from the list ~num~.
 * Solution 2
   #+begin_src python
-    In []:  A[:] = [11, 12, 13, 14, 15]
-  #+end_src
-* squares.png
-  #+begin_latex
-    \begin{center}
-      \includegraphics[scale=0.6]{squares}    
-    \end{center}
-  #+end_latex
+    num[::3]  
+  #+end_src python
 * Question 3
-  - obtain ~[22, 23]~ from ~C~. 
-  - obtain ~[11, 21, 31, 41]~ from ~C~. 
-  - obtain ~[21, 31, 41, 0]~.   
+  Given a list of marks of students in an examination, obtain a list
+  with marks in descending order.
+  #+begin_src python
+    marks = [99, 67, 47, 100, 50, 75, 62]
+  #+end_src python
 * Solution 3
   #+begin_src python
-    In []:  C[1, 1:3]
-    In []:  C[0:4, 0]
-    In []:  C[1:5, 0]
-  #+end_src
-* Question 4
-  Obtain ~[[23, 24], [33, -34]]~ from ~C~
-* Solution 4
-  #+begin_src python
-    In []:  C[1:3, 2:4]
-  #+end_src
-* Question 5
-  Obtain the square in the center of the image
-* Solution 5
+    sorted(marks)[::-1]
+  #+end_src python
+OR
   #+begin_src python
-    In []: imshow(I[75:225, 75:225])
-  #+end_src
-* Question 6
-  Obtain the following
-  #+begin_src python
-    [[12, 0], [42, 0]]
-    [[12, 13, 14], [0, 0, 0]]
-  #+end_src
+    sorted(marks, reverse=True)
+  #+end_src python
 
-* Solution 6
-  #+begin_src python
-    In []: C[::3, 1::3]
-    In []: C[::4, 1:4]
-  #+end_src
 * Summary
-  You should now be able to --
-  - Manipulate 1D \& Multi dimensional arrays
-      - Access and change individual elements 
-      - Access and change rows and columns 
-      - Slice and stride on arrays
-  - Read images into arrays and manipulate them.
+  In this tutorial session we learnt
+    + Obtaining parts of lists using slicing and striding
+    + List concatenation
+    + Sorting lists 
+    + Reversing lists
+
 * Thank you!
 #+begin_latex
   \begin{block}{}
--- a/manipulating-lists/slides.tex	Tue Nov 09 15:33:47 2010 +0530
+++ b/manipulating-lists/slides.tex	Tue Nov 09 16:07:33 2010 +0530
@@ -1,95 +1,141 @@
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%Tutorial slides on Python.
-%
-% Author: FOSSEE 
-% Copyright (c) 2009, FOSSEE, IIT Bombay
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-\documentclass[14pt,compress]{beamer}
-%\documentclass[draft]{beamer}
-%\documentclass[compress,handout]{beamer}
-%\usepackage{pgfpages} 
-%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
-
-% Modified from: generic-ornate-15min-45min.de.tex
-\mode<presentation>
-{
-  \usetheme{Warsaw}
-  \useoutertheme{infolines}
-  \setbeamercovered{transparent}
-}
-
-\usepackage[english]{babel}
+% Created 2010-11-09 Tue 16:07
+\documentclass[presentation]{beamer}
 \usepackage[latin1]{inputenc}
-%\usepackage{times}
 \usepackage[T1]{fontenc}
-
-\usepackage{ae,aecompl}
-\usepackage{mathpazo,courier,euler}
-\usepackage[scaled=.95]{helvet}
+\usepackage{fixltx2e}
+\usepackage{graphicx}
+\usepackage{longtable}
+\usepackage{float}
+\usepackage{wrapfig}
+\usepackage{soul}
+\usepackage{textcomp}
+\usepackage{marvosym}
+\usepackage{wasysym}
+\usepackage{latexsym}
+\usepackage{amssymb}
+\usepackage{hyperref}
+\tolerance=1000
+\usepackage[english]{babel} \usepackage{ae,aecompl}
+\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
+\usepackage{listings}
+\lstset{language=Python, basicstyle=\ttfamily\bfseries,
+commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
+showstringspaces=false, keywordstyle=\color{blue}\bfseries}
+\providecommand{\alert}[1]{\textbf{#1}}
 
-\definecolor{darkgreen}{rgb}{0,0.5,0}
-
-\usepackage{listings}
-\lstset{language=Python,
-    basicstyle=\ttfamily\bfseries,
-    commentstyle=\color{red}\itshape,
-  stringstyle=\color{darkgreen},
-  showstringspaces=false,
-  keywordstyle=\color{blue}\bfseries}
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Macros
-\setbeamercolor{emphbar}{bg=blue!20, fg=black}
-\newcommand{\emphbar}[1]
-{\begin{beamercolorbox}[rounded=true]{emphbar} 
-      {#1}
- \end{beamercolorbox}
-}
-\newcounter{time}
-\setcounter{time}{0}
-\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
-
-\newcommand{\typ}[1]{\lstinline{#1}}
-
-\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
-
-% Title page
-\title{Your Title Here}
-
-\author[FOSSEE] {FOSSEE}
-
-\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
+\title{Manipulating Lists}
+\author{FOSSEE}
 \date{}
 
-% DOCUMENT STARTS
+\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
 \begin{document}
 
+\maketitle
+
+
+
+
+
+
+
+
+
 \begin{frame}
-  \maketitle
+\frametitle{Outline}
+\label{sec-1}
+
+  In this session we shall be looking at 
+\begin{itemize}
+\item Concatenating lists
+\item Obtaining parts of lists
+\item Sorting lists
+\item Reversing lists
+\end{itemize}
 \end{frame}
+\begin{frame}
+\frametitle{Question 1}
+\label{sec-2}
 
+  Obtain the primes less than 10, from the list \texttt{primes}. 
+\end{frame}
 \begin{frame}[fragile]
-  \frametitle{Outline}
-  \begin{itemize}
-    \item 
-  \end{itemize}
+\frametitle{Solution 1}
+\label{sec-3}
+
+\lstset{language=Python}
+\begin{lstlisting}
+primes[0:4]
+\end{lstlisting}
 \end{frame}
+\begin{frame}[fragile]
+\frametitle{Slicing}
+\label{sec-4}
+
+\lstset{language=Python}
+\begin{lstlisting}
+p[start:stop]
+\end{lstlisting}
+\begin{itemize}
+\item Returns all elements of \texttt{p} between \texttt{start} and \texttt{stop}
+\item The element with index equal to \texttt{stop} is \textbf{not} included.
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Question 2}
+\label{sec-5}
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%              All other slides here.                  %%
-%% The same slides will be used in a classroom setting. %% 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  Obtain all the multiples of three from the list \texttt{num}.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 2}
+\label{sec-6}
 
+\lstset{language=Python}
+\begin{lstlisting}
+num[::3]
+\end{lstlisting}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Question 3}
+\label{sec-7}
+
+  Given a list of marks of students in an examination, obtain a list
+  with marks in descending order.
+\lstset{language=Python}
+\begin{lstlisting}
+marks = [99, 67, 47, 100, 50, 75, 62]
+\end{lstlisting}
+\end{frame}
 \begin{frame}[fragile]
-  \frametitle{Summary}
-  \begin{itemize}
-    \item 
-  \end{itemize}
+\frametitle{Solution 3}
+\label{sec-8}
+
+\lstset{language=Python}
+\begin{lstlisting}
+sorted(marks)[::-1]
+\end{lstlisting}
+OR
+\lstset{language=Python}
+\begin{lstlisting}
+sorted(marks, reverse=True)
+\end{lstlisting}
 \end{frame}
-
 \begin{frame}
-  \frametitle{Thank you!}  
+\frametitle{Summary}
+\label{sec-9}
+
+  In this tutorial session we learnt
+\begin{itemize}
+\item Obtaining parts of lists using slicing and striding
+\item List concatenation
+\item Sorting lists
+\item Reversing lists
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Thank you!}
+\label{sec-10}
+
   \begin{block}{}
   \begin{center}
   This spoken tutorial has been produced by the