1 / 11

Lists – Indexing and Sorting

Lists – Indexing and Sorting. Intro to Computer Science CS1510 Dr. Sarah Diesburg. The Python List Data Structure. A list an ordered sequence of items of any type. myList = [1, [2,1], ‘hello’]. List Indexing. We can index into a list in much the same way as a string

bmundy
Download Presentation

Lists – Indexing and Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists – Indexing and Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

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

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

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

  5. 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??

  6. 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()

  7. 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’]

  8. 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.

  9. 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.

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

  11. Using lists to keep information together • Suppose we wanted to create a teacher grading program • Enter student name and scores together • How can we do that with a list? • Keep a list of lists! • Keep the score with the name • [[90,’Ana’] ,[85,’Bob’], [79,’Charli’], [84,‘Dany’]]

More Related