Lists and Tuples Intro to Computer Science CS

  • Slides: 16
Download presentation
Lists and Tuples Intro to Computer Science CS 1510 Dr. Sarah Diesburg

Lists and Tuples Intro to Computer Science CS 1510 Dr. Sarah Diesburg

Homework Assignment PA 07 Regular deadline is Monday - 11/3 Remember Follow the new

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

The Python List Data Structure A list an ordered sequence of items of any

The Python List Data Structure A list an ordered sequence of items of any type. my. List = [1, [2, 1], ‘hello’]

List Indexing We can index into a list in much the same way as

List Indexing We can index into a list in much the same way as a string >>> my. List = [1, [2, 1], ‘hello’] >>> my. List[2] ‘hello’ Let’s play with some indexing problems. Break up into teams of 2 or 3. 4

Example Program my. List = [[‘Penny’, 90], [‘Leonard’, 85], 2] print(my. List[0]) print(my. List[0][0][3]

Example Program my. List = [[‘Penny’, 90], [‘Leonard’, 85], 2] print(my. List[0]) print(my. List[0][0][3] + my. List[1][0][2]) print((my. List[0][1] + my. List[1][1]) / my. List[2]) #What is the output of the three print statements? 5

List Methods my. List. append(x) my. List. extend(C) my. List. pop() my. List. insert(i,

List Methods my. List. append(x) my. List. extend(C) my. List. pop() my. List. insert(i, x) my. List. remove(x) my. List. sort() my. List. reverse() my. List. index(x) my. List. count(x) Which methods modify the list? ? 6

Sorting Only lists have a built-in sorting method. Thus you often convert your data

Sorting Only lists have a built-in sorting method. Thus you often convert your data to a list if it needs sorting: my. Lst = list(‘xyzabc’) my. Lst [‘x’, ’y’, ’z’, ’a’, ’b’, ’c’] my. Lst. sort() # convert back to a string sort. Str = ‘’. join(my. Lst) ‘abcxyz’

Sorted Function The sorted function will break a sequence into elements and sort the

Sorted Function The sorted function will break a sequence into elements and sort the sequence, placing the results in a list: sort. Lst = sorted(‘hi mom’) [‘ ‘, ’h’, ’i’, ’m’, ’o’]

Unusual Results my. Lst = [4, 7, 1, 2] my. Lst = my. Lst.

Unusual Results my. Lst = [4, 7, 1, 2] my. Lst = my. Lst. sort() my. Lst 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.

Anagram Example Anagrams are words that contain the same letters in a different order.

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.

Anagram Program Algorithm 1. 2. 3. Input 2 words to examine. Sort the letters

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

Tuples

Tuples

Tuples are easy: they are simply immutable lists. They are designated with a comma:

Tuples are easy: they are simply immutable lists. They are designated with a comma: Not the parenthesis! my. Tuple = (1, ’a’, 3. 14, True) new. Tuple = (, ) #Empty tuple another. Tuple = 2, 3 #Tuple without parenthesis

The Question is, Why? The real question is, why have an immutable list, a

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.

Lists and Tuples Everything that works with a list works with a tuple except

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.

Anagram Example with Tuples Let’s see if we can modify our anagram program to

Anagram Example with Tuples Let’s see if we can modify our anagram program to use tuples 16