Download presentation
Presentation is loading. Please wait.
Published byEmilie Urbanová Modified over 5 years ago
1
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output: list of new prices Can solve it recursively. Or can use List comprehensions. Syntax for list comprehension: [x*0.8 for x in old_price] >>> price = [10, 20, 30, 100] >>> [x*0.8 for x in price] [8.0, 16.0, 24.0, 80.0] List comprerehsion is a transformation applied to every element of the list. The result of this operation is a list containing transformed elements.
2
List Comprehensions >>> [ 2*x for x in [0,1,2,3,4,5] ]
What is going on here? List Comprehensions >>> [ 2*x for x in [0,1,2,3,4,5] ] [0, 2, 4, 6, 8, 10] >>> [ y**2 for y in range(6) ] [0, 1, 4, 9, 16, 25] >>> [ c == 'a' for c in 'go away!' ] [False, False, False, True, False, True, False, False] Double all prices Give me a square of all numbers from 0 to 6 >>> [x for x in 'go away!’ if x == ‘a’] [‘a’, ‘a’]
3
List Comprehensions >>> [ 2*x for x in [0,1,2,3,4,5] ]
[0, 2, 4, 6, 8, 10]] Any operation you want to apply to each element of the list name that takes on the value of each element in turn the list (or string) any name is OK! >>> [ y**2 for y in range(6) ] [0, 1, 4, 9, 16, 25] >>> [ c == 'a' for c in 'go away!' ] [False, False, False, True, False, True, False, False] >>> [x for x in 'go away!’ if x == ‘a’] [‘a’, ‘a’]
4
Raw recursion vs. list comprehensions
my_len(t) def my_len(t): if t == []: return 0 else: return 1 + my_len(t[1:]) def my_len(t): list_comp = [1 for x in t] return sum(list_comp) listComp = [1 for x in list] return sum(listComp) For every element of the list, we are going to generate a 1, that is, a marker that the element is present. It’s like I am going to put a pebble in a jar for every element that I see. What remains then is to just count the pebbles in the end. ‘’’ or simply ‘’’ def my_len(t): return sum([1 for x in t])
5
Raw recursion vs. list comprehensions
count_vows(s) # of vowels def count_vows(s): if len(s) == 0: return 0 else: if s[0] not in 'aeiou': return count_vows(s[1:]) return 1 + count_vows(s[1:]) Lst= [x in ‘aeiou’ for x in s] The operation is applied to each element of the string. Will generate a list with containing True value for each vowel and False value for each consonant. Interestingly, in Python True value corresponds to integer 1, and False value corresponds to integer 0. So again, I have a one, or a pebble, for each vowel, and it remains to count them. def count_vows(s): return sum([1 for x in s if x in ‘aeiou’])
6
List comprehension with filtering
def only_evens(t): return [x for x in t if is_even(x)] list comprehension with filter >>>only_evens([13, 2, 5, 6, 9]) [2, 6] Note: isEven def isEven(n): return n %2 == 0 The result is also a list but now it’s filtered, only elements that satisfy if condition are making it to the final list
7
More examples of comprehensions
Generate all powers of 2 from 0 to 10 my_list = [2** i for i in range (10) ] # [1 ,2 ,4 ,8 ,16 ,...2^9] Given a list, get a list of square roots of its elements from math import sqrt my_list = [sqrt (x) for x in otherlist ] # produced a squared list Generate a list of odd numbers from 0 to 10 – first let’s generate a list of all numbers Then generate a list of odd numbers If there is time: demonstrate why ‘list’ is not a good name for the list Remind that next class is going to be in the lab Interesting. Generate a list of odd numbers from 0 to 10 list = [x for x in range (10) if x % 2 == 1] # [1, 3, 5, 7, 9]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.