Data Structures: Lists

Slides:



Advertisements
Similar presentations
Lists Ruth Anderson CSE 140 University of Washington 1.
Advertisements

Container Types in Python
UW CSE 190p Section 7/12, Summer 2012 Dun-Yu Hsiao.
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
Data Structures UW CSE 190p Summer >>> xs = range(3) >>> xs = [1,2,3] >>> xs = [‘a’,’b’,’c’] >>> xs = [1, ‘a’, 3] >>> xs = [[1,2,3], [‘a’,’b’,’c’]]
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
Starter – Homework What were our findings?. Computer Science Constants_variables and data types 2.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Lists CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
Lists Michael Ernst CSE 140 University of Washington.
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.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
Embedded Software Development with Python and the Raspberry Pi
Ruth Anderson University of Washington CSE 160 Spring 2015
List comprehensions (and other shortcuts) UW CSE 160 Winter 2016.
Chapter 10 Lists.
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
Lecture 10 Data Collections
Lecture 16: Lists (cont.) and File Input
Engineering Computing
Collections Michael Ernst UW CSE 140.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
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
CSc 110, Spring 2018 Lecture 33: Dictionaries
Lists – Indexing and Sorting
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Ruth Anderson University of Washington CSE 160 Winter 2017
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
Data Structures – 1D Lists
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
Python Data Structures
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
Lists – Indexing and Sorting
CSCE 590 Web Scraping: Lecture 2
Lists Part 1 Taken from notes by Dr. Neil Moore
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
15-110: Principles of Computing
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Module 8 – Searching & Sorting Algorithms
Bryan Burlingame Halloween 2018
Lists – Indexing and Sorting
CSCE 590 Web Scraping: Lecture 2
CSE 231 Lab 6.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMPUTER SCIENCE PRESENTATION.
Python List.
Introduction to Computer Science
Presentation transcript:

Data Structures: Lists UW CSE 190p Summer 2012

Motivating data structures Problem: Compute the number of unique words in a document first attempt: doc = list_of_words number_of_words = len(list_of_words) print number_of_words

Motivating data structures Problem: Compute the number of unique words in a document doc = list_of_words number_of_words = 0 for word in doc: if not already_seen_it(word): number_of_words = number_of_words + 1 wishful thinking “wishful thinking” remember: first write the program you wish you had!

Motivating data structures Problem: Compute the number of unique words in a document doc = list_of_words number_of_words = 0 scratchpad = [] for word in doc: if not already_seen_it(word, scratchpad): save(word, scratchpad) number_of_words = number_of_words + 1

Motivating data structures Problem: Compute the number of unique words in a document doc = list_of_words number_of_words = 0 scratchpad = [] for word in doc: if not (word in scratchpad): number_of_words = number_of_words + 1 scratchpad.append(word)

Motivating data structures Problem: Compute the number of unique words in a document doc = list_of_words number_of_words = 0 scratchpad = set() # a set! for word in doc: scratchpad.add(word) print len(scratchpad)

Data Structures A data structure is way of organizing data such that certain operations are convenient or efficient Example: What operations are efficient with a file cabinet sorted by date? a shoe box?

Exercise Write a function to compute the position of the first occurrence of the element value in a list somelist def index(value, somelist): i = 0 for c in somelist: if c == value: return i i = i + 1 return None

Lists A list is a sequence of elements What operations should a list support efficiently and conveniently?

List Basics (demo)

“method syntax” >>> mylist = [10,20,30] >>> mylist.append(4) # think “append(mylist, 4)” >>> mylist.index(10) >>> mylist.sort() >>>

List methods (demo)

Other list methods list.index(x) list.sort() list.reverse() Return the index in the list of the first item whose value is x. It is an error if there is no such item. list.sort() Sort the items of the list, in place. list.reverse() Reverse the elements of the list, in place. list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

More list methods list.extend(L) list.count(x) list.remove(x) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. list.count(x) Return the number of times x appears in the list. list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

Exercise: Convert Units ctemps = [17.1, 22.3, 18.4, 19.1] ftemps = [] for c in ctemps: f = celsius_to_farenheit(c) ftemps.append(f) ftemps = [celsius_to_farenehit(c) for c in ctemps] doubles = [i*2 for i in range(5)]

Sets What’s a set? What’s the difference between a set and a list? What operations should a set support efficiently and conveniently?