Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
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 Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
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.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Python Control of Flow.
Python, Part 2. Python Object Equality x == y x is y In Java: (x.equals(y)) (x == y)
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
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
Concurrent Algorithms. Summing the elements of an array
Built-in Data Structures in Python An Introduction.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python iterators and generators. Iterators and generators  Python makes good use of iterators  And has a special kind of generator function that is.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
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.
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.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
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,
CS314 – Section 5 Recitation 10
Higher Order Functions
Introduction to Higher Order (Functional Programming) (Python) part 2
Lecture 35: List Comprehensions
Python: Control Structures
Dictionaries, File operations
Lecture 34: List Comprehensions
Concurrent Algorithms
Python Classes By Craig Pennell.
CHAPTER FOUR Functions.
while loops; file I/O; introduction to lists
Arithmetic operations, decisions and looping
Python Primer 2: Functions and Control Flow
Concurrent Algorithms
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.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
Dictionaries Dictionary: object that stores a collection of data
(more) Python.
Lambda Functions, MapReduce and List Comprehensions
The structure of programming
Topics Sequences Introduction to Lists List Slicing
Concurrent Algorithms
Functions Functions being objects means functions can be passed into other functions: sort (list, key=str.upper)
Functional Programming
Concurrent Algorithms
Concurrent Algorithms
Lecture 20 – Practice Exercises 4
Python iterators and generators
Presentation transcript:

Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! 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 Iterator Two special methods: __iter__ and __next__ __iter__ sets up the iteration __next__ returns values one by one until the end of the sequence is reached raise StopIteration to stop iteration now objects can be the target of for-each loops! may also define method: x.__contains__(y) supports sweet syntax sugar: y in x

3 Iterator Examples >>> class IterList:... def __init__(self):... self.list = [2,4,6,8] def __iter__(self):... self.iter_count = 0... return self def __next__(self):... if (self.iter_count >= len(self.list)):... raise StopIteration... return self.list[self.iter_count] >>> list = IterList() >>> for element in list:... print(element*2) [4, 8, 12, 16]

4 Map map( function, iterable,... ) Map applies function to each element of iterable and creates a list of the results You can optionally provide more iterables as parameters to map and it will place tuples in the result list Map returns an iterator which can be cast to list

5 Map Examples >>> nums = [0, 4, 7, 2, 1, 0, 9, 3, 5, 6, 8, 0, 3] >>> nums = list(map(lambda x : x % 5, nums)) >>> print(nums) [0, 4, 2, 2, 1, 0, 4, 3, 0, 1, 3, 0, 3] >>> def even (x):... if (x % 2 == 0):... return "even"... else:... return "odd" >>> list (map(even, nums)) ['even', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'odd', 'odd', 'even', 'even', 'even', 'odd']

6 Functions as Parameters >>> list = ['once', 'upon', 'a', 'time', 'in', 'a'] >>> def foo (x):... return x * 3 >>> bar = foo >>> my_map (foo, list) ['onceonceonce', 'uponuponupon', 'aaa', 'timetimetime', 'ininin', 'aaa'] >>> my_map (bar, list) ['onceonceonce', 'uponuponupon', 'aaa', 'timetimetime', 'ininin', 'aaa'] Functions can be assigned to variables and/or passed as parameters

7 Map Code >>> def my_map (fun, list):... nlist = []... for item in list:... nlist.append(fun(item))... return nlist

8 Reduce reduce( function, iterable [, initializer ]) Reduce will apply function to each element in iterable along with the sum so far and create a cumulative sum of the results function must take two parameters If initializer is provided, initializer will stand as the first argument in the sum Unfortunately in python 3 reduce() requires an import statement from functools import reduce

9 Reduce Examples >>> nums = [9, 2, 0, -4, 0, 0, 7, 5, 3, 8] >>> reduce(lambda sum, current: sum + current, nums) 30 >>> foo = ['in', 'a', 'galaxy', 'far', 'far', 'away'] >>> reduce(lambda sum, current : sum + current, foo) 'inagalaxyfarfaraway' >>> reduce(lambda x,y : x+y + “ “, foo, “once upon a time ”) 'once upon a time in a galaxy far far away '

10 Reduce Examples >>> numlists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] >>> reduce(lambda sum, current: sum+current, numlists, []) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> nums = [1, 2, 3, 4, 5, 6, 7, 8] >>> nums = list(reduce(lambda x, y : (x, y), nums)) >>> print(nums) (((((((1, 2), 3), 4), 5), 6), 7), 8)

11 Reduce Problem Goal: given a list of numbers I want to find the average of those numbers in a few lines using reduce For Loop Method: - sum up every element of the list - divide the sum by the length of the list

12 Reduce Problem Solution >>> nums = [92, 27, 63, 43, 88, 8, 38, 91, 47, 74,... 18, 16, 29, 21, 60, 27, 62, 59, 86, 56] average = reduce(lambda x, y : x + y, nums) / len(nums)

13 MapReduce Framework for processing huge datasets on certain kinds of distributable problems Map Step: - master node takes the input, chops it up into smaller and distributes to smaller nodes - smaller nodes may continue chopping recursively Reduce Step: - master node then takes the answers to all the sub- problems and combines them in a way to get the desired output

14 MapReduce Problem: Given an how do you tell if it is spam? Count occurrences of certain words. If they occur too frequently the is spam.

15 MapReduce >>> = “Well, there's egg and bacon; egg sausage and bacon; egg and spam; egg bacon and spam; egg bacon sausage and spam; spam bacon sausage and spam; spam egg spam spam bacon and spam; spam sausage spam spam bacon spam tomato and spam;” >>> = .split() >>> def spamWeight (word): if ("spam" in word): return 1 else: return 0 >>> list(map (spamWeight, )) [0, 0,... 1, 0, 0, 1] >>> reduce(lambda x, xs: x + xs, map(spamWeight, )) 14

16 Dictionaries map keys, which can be any immutable type, to values, which can be any type >>> # declaring an empty dictionary >>> eng2sp = {} >>> # adding values >>> eng2sp['one'] = 'uno' >>> eng2sp['two'] = 'dos' >>> # declaring dictionary with initial values >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three':'tres'}

17 Dictionaries Operations: - print, del, len, in Methods: - keys(), values(), items() 1212 >>> eng2sp.items() [('three', 'tres'), ('two', 'dos'), ('one', 'uno')]

18 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

19 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 vec] [[2, 4], [4, 16], [6, 36]] >>> [x, x**2 for x in vec] SyntaxError: invalid syntax (requires parens)

20 List Comprehensions You can do most things that you can do with map, filter and reduce more nicely with list comprehensions The spam program from earlier using list comprehensions: >>> = ['once', 'upon', 'a', 'time', 'in', 'a', 'far'] >>> len( [1 for x in if x == 'a'] ) 2