Ruth Anderson UW CSE 160 Spring 2018

Slides:



Advertisements
Similar presentations
List comprehensions UW CSE 140 Winter Ways to express a list 1.Explicitly write the whole thing: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81,
Advertisements

Lists Ruth Anderson CSE 140 University of Washington 1.
Course A201: Introduction to Programming 10/28/2010.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
UW CSE 190p Section 7/12, Summer 2012 Dun-Yu Hsiao.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Sorting Ruth Anderson UW CSE 140 Winter Sorting hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split()
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Tuples Chapter 10 Python for Informatics: Exploring Information
Sorting Ruth Anderson UW CSE 160 Spring sorted vs. sort hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split()
Collections Michael Ernst CSE 190p University of Washington.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Lists Ruth Anderson University of Washington CSE 160 Winter
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Compsci 6/101, Spring Why is sorting data important? l Guess a number in [1,100] --- I'll tell you yes or no  How many guesses needed worst-case?
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Algorithmic complexity: Speed of algorithms
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
Ruth Anderson University of Washington CSE 160 Spring 2015
Ruth Anderson CSE 140 University of Washington
Tuples Chapter 10 Python for Everybody
Ruth Anderson UW CSE 160 Spring 2018
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
Lists Part 1 Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Spring 2015
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
Ruth Anderson University of Washington CSE 160 Winter 2017
Sharing, mutability, and immutability
4. sequence data type Rocky K. C. Chang 16 September 2018
Python for Informatics: Exploring Information
Sharing, mutability, and immutability
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithmic complexity: Speed of algorithms
Michael Ernst UW CSE 140 Winter 2013
Linear Search Binary Search Tree
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CS 1111 Introduction to Programming Spring 2019
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithmic complexity: Speed of algorithms
Ruth Anderson CSE 160 University of Washington
Module 8 – Searching & Sorting Algorithms
Topics Sequences Introduction to Lists List Slicing
Ruth Anderson UW CSE 160 Spring 2018
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
CSE 231 Lab 6.
CSE 231 Lab 7.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python - Tuples.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Ruth Anderson UW CSE 160 Spring 2018 Sorting Ruth Anderson UW CSE 160 Spring 2018

sorted vs. sort sorted(itr) - is a function that takes an iterable as a parameter (e.g. sequence types: list, string, tuple) and returns a sorted version of that parameter lst.sort - is a method that sorts the list that it is called on in-place (and returns None). .sort can only be called on lists my_lst = [5, 3, 4, 2] print sorted(my_lst)  [2, 3, 4, 5] print my_lst  [5, 3, 4, 2] my_lst.sort() print my_lst  [2, 3, 4, 5] Returns a new sorted list Does not modify original list Modifies the list in place, returns None

sorted vs. sort example See in python tutor hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split() print "hamlet:", hamlet print "sorted(hamlet):", sorted(hamlet) print "hamlet.sort():", hamlet.sort() Lists are mutable – they can be changed including by functions Returns a new sorted list (does not modify the original list) Modifies the list in place, returns None

Customizing the sort order See in python tutor Goal: sort a list of names by last name names = ["Isaac Newton", "Albert Einstein", "Niels Bohr", "Marie Curie", "Charles Darwin", "Louis Pasteur", "Galileo Galilei", "Margaret Mead"] print "names:", names This does not work: print "sorted(names):", sorted(names) When sorting, how should we compare these names? "Niels Bohr" "Charles Darwin" names: ['Isaac Newton', 'Albert Einstein', 'Niels Bohr', 'Marie Curie', 'Charles Darwin', 'Louis Pasteur', 'Galileo Galilei', 'Margaret Mead'] sorted(names): ['Albert Einstein', 'Charles Darwin', 'Galileo Galilei', 'Isaac Newton', 'Louis Pasteur', 'Margaret Mead', 'Marie Curie', 'Niels Bohr'] Goodall, Jane, English Mead, Margaret,  Marie Curie

Sort key See in python tutor A sort key is a function that can be called on each list element to extract/create a value that will be used to make comparisons. We can use this to sort on a value (e.g. “last_name”) other than the actual list element (e.g. “first_name last_name”). We could use the following function as a sort key to help us sort by last names: def last_name(str): return str.split(" ")[1] print 'last_name("Isaac Newton"):', last_name("Isaac Newton")

Use a sort key as the key argument See in python tutor Supply the key argument to the sorted function or the sort function def last_name(str): return str.split(" ")[1] names = ["Isaac Newton", "Ada Lovelace", "Fig Newton", "Grace Hopper"] print sorted(names, key=last_name) print sorted(names, key=len) def last_name_len(name): return len(last_name(name)) print sorted(names, key=last_name_len) If there is a tie in last names, preserves original order of values. def last_name(str): return str.split(" ")[1] names = ["Isaac Newton", "Fig Newton", "Niels Bohr"] print "sorted(names, key = last_name):" print sorted(names, key = last_name) print "sorted(names, key = last_name, reverse = True):" print sorted(names, key = last_name, reverse = True) print "sorted(names, key = len):" print sorted(names, key = len) def last_name_len(name): return len(last_name(name)) print "sorted(names, key = last_name_len):" print sorted(names, key = last_name_len) sorted(names, key = last_name): ['Niels Bohr', 'Isaac Newton', 'Fred Newton'] sorted(names, key = last_name, reverse = True): ['Isaac Newton', 'Fred Newton', 'Niels Bohr'] ['Niels Bohr', 'Fred Newton', 'Isaac Newton']

itemgetter is a function that returns a function See in python tutor itemgetter is a function that returns a function Useful for creating a function that will return particular elements from a sequence (e.g. list, string, tuple): import operator operator.itemgetter(2)([7, 3, 8])  8 operator.itemgetter(0)([7, 3, 8])  7 operator.itemgetter(1)([7, 3, 8])  3 operator.itemgetter(0, 1)([7, 3, 8])  (7, 3) operator.itemgetter(3)([7, 3, 8])  IndexError: list index out of range Returns a function Call function passing in this list as an argument A tuple

Itemgetter Example See in python tutor import operator operator.itemgetter(2, 7, 9, 10)("dumbstricken") operator.itemgetter(2, 5, 7, 9)("homesickness") operator.itemgetter(2, 7, 9, 10)("pumpernickel") operator.itemgetter(2, 3, 6, 7)("seminaked") operator.itemgetter(1, 2, 4, 5)("smirker") # Could even return elements in a different order operator.itemgetter(9, 7, 6, 1)("beatnikism") operator.itemgetter(14, 13, 5, 1)("Gedankenexperiment") operator.itemgetter(12, 10, 9, 5)("mountebankism") Returns a function Call function passing in this string as an argument Print all of these: <operator.itemgetter object at 0x7f5f390a0910> ('m', 'i', 'k', 'e')

Two ways to Import itemgetter A tuple import operator student_score = ('Robert', 8) operator.itemgetter(0)(student_score)  “Robert” operator.itemgetter(1)(student_score)  8 Or from operator import itemgetter itemgetter(0)(student_score)  “Robert” itemgetter(1)(student_score)  8 Another way to import, allows you to call itemgetter directly.

Using itemgetter Sort the list by name: Sort the list by score See in python tutor Another way to import, allows you to call itemgetter directly. from operator import itemgetter student_score = ('Robert', 8) itemgetter(0)(student_score)  “Robert” itemgetter(1)(student_score)  8 student_scores = [('Robert', 8), ('Alice', 9), ('Tina', 7)] Sort the list by name: sorted(student_scores, key=itemgetter(0)) Sort the list by score sorted(student_scores, key=itemgetter(1))

Sorting based on two criteria See in python tutor Goal: sort based on score; if there is a tie within score, sort by name Two approaches: Approach #1: Use an itemgetter with two arguments Approach #2: Sort twice (most important sort last) student_scores = [('Robert', 8), ('Alice', 9), ('Tina', 10), ('James', 8)] Approach #1: sorted(student_scores, key=itemgetter(1,0)) Approach #2: sorted_by_name = sorted(student_scores, key=itemgetter(0)) sorted_by_score = sorted(sorted_by_name, key=temgetter(1))

Sort on most important criteria LAST Sorted by score (ascending), when there is a tie on score, sort using name from operator import itemgetter student_scores = [('Robert', 8), ('Alice', 9), ('Tina', 10), ('James', 8)] sorted_by_name = sorted(student_scores, key=itemgetter(0)) >>> sorted_by_name [('Alice', 9), ('James', 8), ('Robert', 8), ('Tina', 10)] sorted_by_score = sorted(sorted_by_name, key=itemgetter(1)) >>> sorted_by_score [('James', 8), ('Robert', 8), ('Alice', 9), ('Tina', 10)]

More sorting based on two criteria See in python tutor If you want to sort different criteria in different directions, you must use multiple calls to sort or sorted student_scores = [('Robert', 8), ('Alice', 9), \ ('Tina', 10), ('James', 8)] Goal: sort score from highest to lowest; if there is a tie within score, sort by name alphabetically (= lowest to highest) sorted_by_name = sorted(student_scores, key=itemgetter(0)) sorted_by_hi_score = sorted(sorted_by_name, key=itemgetter(1), reverse=True) Remember: Sort on most important criteria LAST

Digression: Lexicographic Order 'Aaron' 'Andrew' 'Angie' [1, 9, 9] [2, 1] [3] 'with' 'withhold' 'withholding' [1] [1, 1] [1, 1, 1] Each of these lists is in lexicographic order 'Able' 'Charlie' 'baker' 'delta' [1, 1] [1, 1, 2] [1, 2]

Sorting: strings vs. numbers Sorting the powers of 5: >>> sorted([125, 5, 3125, 625, 25]) [5, 25, 125, 625, 3125] >>> sorted(["125", "5", "3125", "625", "25"]) ['125', '25', '3125', '5', '625']

Aside: Use a sort key to create a new list See in python tutor Create a different list that contains the value returned by the sort key, sort it, then extract the relevant part: names = ["Isaac Newton", "Fig Newton", "Niels Bohr"] # keyed_names is a list of [lastname, fullname] lists keyed_names = [] for name in names: keyed_names.append([last_name(name), name]) sorted_keyed_names = sorted(keyed_names) sorted_names = [] for keyed_name in sorted_keyed_names: sorted_names.append(keyed_name[1]) print "sorted_names:", sorted_names 1) Create the new list. 2) Sort the list new list. If there is a tie in last names, sort by next item in list: fullname Take a look at the list you created, it can now be sorted: print "keyed_names:", keyed_names print "sorted(keyed_names):", sorted(keyed_names) Or sorted in reverse: print "sorted(keyed_names, reverse = True):" print sorted(keyed_names, reverse = True) (This works because Python compares two elements that are lists elementwise.) Output: keyed_names: [['Newton', 'Isaac Newton'], ['Newton', 'Fred Newton'], ['Bohr', 'Niels Bohr']] sorted(keyed_names): [['Bohr', 'Niels Bohr'], ['Newton', 'Fred Newton'], ['Newton', 'Isaac Newton']] sorted(keyed_names, reverse = True): [['Newton', 'Isaac Newton'], ['Newton', 'Fred Newton'], ['Bohr', 'Niels Bohr']] sorted_names: ['Isaac Newton', 'Fred Newton', 'Niels Bohr'] # keyed_names is a list of [lastname, fullname] lists (tuples would be better!) Aside, instead of: keyed_names.append([last_name(name), name]) You could do: keyed_names = [[last_name(name), name] for name in names] sorted_names.append(keyed_name[1]) sorted_names = [keyed_name[1] for keyed_name in sorted_keyed_names] >>> al = [ [ 1, 17, 32 ], [ 1, 17, 8 ], [ 1, 12, 103] ] >>> sorted(al) [[1, 12, 103], [1, 17, 8], [1, 17, 32]] 3) Extract the relevant part.