Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Course A201: Introduction to Programming 10/28/2010.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
Objectives of these slides:
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Containers Overview and Class Vector
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
If statements while loop for loop
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.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Built-in Data Structures in Python An Introduction.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
1 the hash table. hash table A hash table consists of two major components …
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
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.
Java Collections Framework The client view. The Big Picture.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Higher Order Functions
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
i206: Lecture 10: Lists, Stacks, Queues
LING 388: Computers and Language
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
MapReduce, Dictionaries, List Comprehensions
while loops; logic; random numbers; tuples
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Lambda Functions, MapReduce and List Comprehensions
Stacks Chapter 5.
Stacks, Queues, and Deques
Stacks.
Sample lecture slides.
Presentation transcript:

Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under:

2 Stack Information found here: html Really easy! Use lists to simulate a stack.append(value) in order to “push” onto the stack.pop() in order to ‘pop’ off the stack. Check this example out!

3 Queue Really easy too!!!! Use lists to simulate a queue.insert(0, value) in order to ‘add’ to the queue..pop() in order to ‘remove’ off from the queue. Why does this work? What do insert and pop really do?

4 Queue Ok… this is dumb… and inefficient. Using lists, we get O(N), but….. from collections import deque queue = deque([‘blah’, ‘blah’, ‘blah’]) Use append() to ‘add’ to the queue Use popleft() to ‘remove’ from the queue This works in O(1)! Sweeeeeeeeet.

5 Dictionary Equivalent to Java’s Map. Stores keys and values. Empty dictionary is defined by: m = {} Pre-populated dictionary: (Map ) (mapping names to SCII division…) m2 = { ‘jordan’ : ‘master’, ‘roy’ : ‘bronze’, ‘marty’ : ‘bronze’ } Add to dictionary: m2[ ‘IMMVP’ ] = ‘master’ Retrieve from dictionary m2[‘jordan’] # ‘master’ Example!!

6 Dictionary How to get keySet() map.keys() Uh oh……. We get something called dict_keys… We should cast to a list. list(map.keys()) How to check if a key is contained in a dictionary… ‘marty’ in m # returns true bc ‘marty’ is in dictionary ‘bronze’ in m # returns false bc ‘bronze’ is not a key How to delete del m[‘marty’] Get values with: m.values() Note that you get the dict_values. So cast to a list. Example!!

7 Set s = { ‘GLaDOS’, ‘Cloud’, ‘Aeris’, ‘Shepard’, ‘Cloud’, ‘Aeris’ } # {‘GLaDOS’, ‘Cloud’, “Aeris’, ‘Shepard’ } Removed duplicates s2 = set(‘hello I am soooo happy!!!’) Creates a set out of all the characters The stuff in the blue must be iterable. i.e. lists, tuples, etc… Check for contains is same as dictionary. ‘GLaDOS’ in s

8 Set Tons of extra functionality Check for subsets, see if set A is contained in set B Go here to see more: (under 5.7)

9 List Comprehensions [expression for element in list] Applies the expression to each element in the list You can have 0 or more for or if statements If the expression evaluates to a tuple it must be in parenthesis

10 List Comprehensions >>> vec = [2, 4, 6] >>> [3*x for x in vec] [6, 12, 18] >>> [3*x for x in vec if x > 3] [12, 18] >>> [3*x for x in vec if x < 2] [] >>> [[x,x**2] for x in range(10)] [[0, 0], [1, 1], [2, 4], [3, 9]] >>> [x, x**2 for x in vec] # error - parens required for tuples

11 List Comprehensions You can do most things that you can do with map, filter and reduce more nicely with list comprehensions How can we find out how many times ‘a’ appears in the list named ? >>> = ['once', 'upon', 'a', 'time', 'in', 'a', 'far', 'away'] >>> len( [1 for x in if x == 'a'] ) >>> 2

12 Example of Map ISA hierarchy