MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.

Slides:



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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
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, 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.
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.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
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.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
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 Functions.
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.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
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.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
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.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
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,
Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
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
CHAPTER FOUR Functions.
Functional.
while loops; file I/O; introduction to lists
Arithmetic operations, decisions and looping
Concurrent Algorithms
MapReduce, Dictionaries, List Comprehensions
Week 7 Lists Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
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.
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
(more) Python.
Lambda Functions, MapReduce and List Comprehensions
CISC101 Reminders All assignments are now posted.
Introduction to Spark.
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
Presentation transcript:

MapReduce, Dictionaries, List Comprehensions 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 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

3 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']

4 Recursion The same as in Java!

5 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

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

7 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

8 Reduce Examples nums = [9, 2, 0, -4, 0, 0, 7, 5, 3, 8] reduce(lambda x, y: x+y, nums) # 30 foo = ['once', 'upon', 'a', 'time', 'in', 'a', 'far', 'away'] reduce(lambda x, y : x + y, foo) # 'onceuponatimeinafaraway'

9 Reduce Examples numlists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] reduce(lambda a, b: a + b, 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)

10 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

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

12 MapReduce Framework for processing huge datasets on certain kinds of distributable problems Map Step: - master node takes the input, chops it up into smaller sub-problems, and distributes those to worker nodes. - worker node may chop its work into yet small pieces and redistribute again

13 MapReduce Reduce Step: - master node then takes the answers to all the sub-problems and combines them in a way to get the 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 = ['the', 'this', 'annoy', 'the', 'the', 'annoy'] >>> def in (x): if (x == "the"): return 1; else: return 0; >>> map (in , l) [1, 0, 0, 0, 1, 1, 0] >>> reduce ((lambda x, xs: x + xs), map(in , )) 3

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() >>> 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] # error - parens required for tuples

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', 'away'] >>> len( [1 for x in if x == 'a'] ) >>> 2