Lists and Tuples - PowerPoint PPT Presentation

About This Presentation
Title:

Lists and Tuples

Description:

... document Example function commenting The Python List Data Structure A list an ordered sequence ... your data to a list if it needs sorting: myLst ... – PowerPoint PPT presentation

Number of Views:1802
Avg rating:3.0/5.0
Slides: 17
Provided by: SystemA410
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Lists and Tuples


1
Lists and Tuples
  • Intro to Computer Science
  • CS1510
  • Dr. Sarah Diesburg

2
Homework Assignment PA07
  • Regular deadline is Monday - 11/3
  • Remember
  • Follow the new design document and function
    commenting
  • Example design document
  • Example function commenting

3
The Python List Data Structure
  • A list an ordered sequence of items of any type.
  • myList 1, 2,1, hello

4
List Indexing
  • We can index into a list in much the same way as
    a string
  • gtgtgt myList 1, 2,1, hello
  • gtgtgt myList2
  • hello
  • Lets play with some indexing problems. Break up
    into teams of 2 or 3.

5
Example Program
  • myList Penny, 90, Leonard, 85, 2
  • print(myList0)
  • print(myList003 myList102)
  • print((myList01 myList11) / myList2)
  • What is the output of the three print
    statements?

6
List Methods
  • myList.append(x)
  • myList.extend(C)
  • myList.pop()
  • myList.insert(i,x)
  • myList.remove(x)
  • myList.sort()
  • myList.reverse()
  • myList.index(x)
  • myList.count(x)

Which methods modify the list??
7
Sorting
  • Only lists have a built-in sorting method. Thus
    you often convert your data to a list if it needs
    sorting
  • myLst list(xyzabc)
  • myLst ? x,y,z,a,b,c
  • myLst.sort()
  • convert back to a string
  • sortStr .join(myLst)
  • ? abcxyz

8
Sorted Function
  • The sorted function will break a sequence into
    elements and sort the sequence, placing the
    results in a list
  • sortLst sorted(hi mom)
  • ? ,h,i,m,m,o

9
Unusual Results
  • myLst 4, 7, 1, 2
  • myLst myLst.sort()
  • myLst ? None what happened?
  • What happened was the sort operation changed the
    order of the list in place (right side of
    assignment). Then the sort method returned None,
    which was assigned to the variable. The list was
    lost and None is now the value of the variable.

10
Anagram Example
  • Anagrams are words that contain the same letters
    in a different order. For example iceman and
    cinema.
  • A strategy to identify anagrams is to take the
    letters of a word, sort those letters, then
    compare the sorted sequences.
  • Anagrams should have the same sequence.

11
Anagram Program Algorithm
  1. Input 2 words to examine.
  2. Sort the letters of each word into a new string.
  3. Compare the resulting sorted strings

12
Tuples
13
Tuples
  • Tuples are easy they are simply immutable lists.
  • They are designated with a comma
  • Not the parenthesis!
  • myTuple (1,a,3.14,True)
  • newTuple (,) Empty tuple
  • anotherTuple 2,3 Tuple without parenthesis

14
The Question is, Why?
  • The real question is, why have an immutable list,
    a tuple, as a separate type?
  • An immutable list gives you a data structure with
    some integrity, some permanency, if you will.
  • You know you cannot accidentally change one.

15
Lists and Tuples
  • Everything that works with a list works with a
    tuple except methods that modify the list/tuple.
  • Thus indexing, slicing, len, print all work as
    expected.
  • However, none of the mutable methods work
    append, extend, remove.

16
Anagram Example with Tuples
  • Lets see if we can modify our anagram program to
    use tuples
Write a Comment
User Comments (0)
About PowerShow.com