UW CSE 190p Section 7/12, Summer 2012 Dun-Yu Hsiao.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

Lists Ruth Anderson CSE 140 University of Washington 1.
Course A201: Introduction to Programming 10/28/2010.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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.
UW CSE 190p Section 6/21, Summer 2012 Dun-Yu Hsiao.
Guide to Programming with Python
 9: Arrays  Why?  What is an Array?  Declaring and Creating an Array  Initializing and Array  Array Operations  Array Examples  Arrays of Objects.
COMS S1007 Object-Oriented Programming and Design in Java July 10, 2008.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
I Power Int 2 Computing Software Development High Level Language Constructs.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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()
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Machaut Messe Performance Performance Performance Isorhythm Isorhythm.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Homework Final writing portfolio due Tuesday, May 24 th. Include: -Outline -Draft 1 with peer feedback -Draft 2 with teacher feedback -Final draft: highlight.
Chapter 2: Equations and Inequalities
I Power Higher Computing Software Development High Level Language Constructs.
UW CSE 190p Section 7/26, Summer 2012 Dun-Yu Hsiao.
Integers and Absolute Value Section 2-1. Intro to Integers An integer is the set of whole numbers and their opposites, including zero, represented by.
© William James Calhoun, : Solving Equations with Addition and Subtraction OBJECTIVE: You need to be able to solve equations by using addition.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Peoplesoft Tips & tricks. Don’t forget to sort your main menu items for an easier search:
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Lists Ruth Anderson University of Washington CSE 160 Winter
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.
22016 AP ® Preadministration Session You need… #2 Pencil Your iPAD if you would like to access a copy of the powerpoint. Answer sheet Student Pack Textbook/notebook.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Absolute Value! Sept. 10 and 11. Bell Quiz! Remove everything from desk except for pencil and calculator.
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
Algorithmic complexity: Speed of algorithms
Containers and Lists CIS 40 – Introduction to Programming in Python
More on Lists.
Data Structures: Lists
Ruth Anderson University of Washington CSE 160 Spring 2015
Ruth Anderson CSE 140 University of Washington
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Ruth Anderson UW CSE 160 Winter 2017
CISC101 Reminders Quiz 2 this week.
Ruth Anderson University of Washington CSE 160 Spring 2018
Python: Array Slicing Damian Gordon.
Ruth Anderson University of Washington CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Ruth Anderson UW CSE 160 Winter 2017
Algorithmic complexity: Speed of algorithms
Michael Ernst UW CSE 140 Winter 2013
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Single Linked List TA.Rawan ALAmri By James Sager – Oct 15, 2017.
Michael Ernst CSE 140 University of Washington
To view, enable editing, select Slide Show, select From Beginning
Algorithmic complexity: Speed of algorithms
Ruth Anderson CSE 160 University of Washington
Introduction to Programming with Python
Ruth Anderson UW CSE 160 Spring 2018
TRIFOLD AREA – THIS GUIDE WILL BE REMOVED BEFORE PRINTING – TRIFOLD AREA – THIS GUIDE WILL BE REMOVED BEFORE PRINTING – TRIFOLD AREA – THIS GUIDE WILL.
Solve for x. 1. x - 2 = x + 12 = x = ½ x = 12
Solve for x. 1. x - 2 = x + 12 = x = ½ x = 12
UW CSE 190p Section 7/19, Summer 2012 Dun-Yu Hsiao.
UW CSE 190p Section 6/28, Summer 2012 Dun-Yu Hsiao.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

UW CSE 190p Section 7/12, Summer 2012 Dun-Yu Hsiao

Homework/Quiz Questions?

Before We Start Create and remember save every code you try today! If you have any question about today’s material, your report to TA.

Outlines List slicing Set Sort

List Slicing test_list = ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7'] Start from the 3rd through the rest of the list: test_list[2:] From beginning through 5th element: test_list[:5]

List Slicing test_list = ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7'] Get last element: test_list[-1] Get last four elements: test_list[-4:] Get everything except last three: test_list[:-3]

List Slicing test_list = ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7'] Reverse the list test_list[::-1] Get the copy of the whole list test_list[:]

new_list1 = test_list[:] new_list2 = test_list print new_list1 print new_list2 test_list.append( 'e8' ) new_list1.append( 'e9' ) new_list2.append( 'e10' ) print new_list1 print new_list2 print test_list ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7'] ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7’] ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e9'] ['e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e10']

Set Operations Using Lists list1 = [1, 2, 3, 4, 5] list2 = [4, 6, 5, 7, 8] Find the common elements in both list using list out1 = [i for i in list2 if i in list1] Find the all the unique elements in both list using list out2 = list1+list2 for i in out1: out2.remove(i) Find all the elements that are not in both lists using list out3 = [i for i in list1+list2 if i not in out1]

Set Operations Using Lists list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 5, 6, 4, 3, 2, 2, 7, 1] list2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 4, 10, 2, 6] Find the common elements in both lists: out1 = [] for i in list2: if i in list1 and i not in out1: out1.append(i) Find all the elements in either list: out2 = [] for i in list1+list2: if i not in out2: out2.append(i) Find all the elements that are in one list but not the other: out3 = [] for i in list1+list2: if i not in out1 and i not in out3: out3.append(i)

Set Operations Using Sets set1 = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 5, 6, 4, 3, 2, 2, 7, 1]) set2 = set([2, 4, 6, 8, 10, 12, 14, 16, 18, 4, 10, 2, 6]) Find the common elements in both list using set set1 & set2 Find the all the unique elements in both list using set set1 | set2 Find all the elements that are not in both lists using set set1 ^ set2 Much shorter, clearer, easier to write!

Sort List of score of student: test_list = [('Robert', 8), ('Alice', 9), ('Tina', 7)] Sort the list by name sorted(test_list, key=itemgetter(0) ) Sort the list by score sorted(test_list, key=itemgetter(1) )

Sort List of score of student: test_list = [('Robert', 8), ('Alice', 9), ('Tina', 10), ('James', 8)] Sort the list by score then name sorted(test_list, key=itemgetter(1,0) ) Sort the list by name in reverse manner sorted(test_list, key=itemgetter(0), reverse=True )

Sort Exercise test_list = [['Robert', 'M', 8], ['Alice', 'F', 9], ['Tina', 'F', 7], ['Tony', 'M', 10], ['Bob', 'M', 9], ['Keith', 'M', 8]] Sort by score, name then gender from high to low sorted(test_list, key=itemgetter(2,0,1), reverse=True ) What if we want the names to go alphabetically? for item in test_list: item[2] = -item[2] sorted_list = sorted(test_list, key=itemgetter(2,0,1) ) for item in sorted_list : item[2] = -item[2]

Questions?