Exercise 1: Using list comprehension, find all of the numbers from 1-105 that are divisible by 7
Exercise 2: Use list comprehension to count the number of spaces in a string
Exercise 3: Use list comprehension to remove all of the vowels in a string
Exercise 4: Run the .split() function we saw last lecture on a string to see what it does when you don't pass any parameters. Then, use this and list comprehension to make a list that only keeps the words that are less than or equal to four letters in the string "Poincaré's homology sphere is a closed 3- manifold with the same homology as the 3-sphere but with a fundamental group which is non-trivial."
Exercise 5: Recall that we can define a matrix using lists of lists. Use a nesting of list comprehension to make a new list that is the transpose of the matrix m = [[1,2,3],[4,5,6],[7,8,9]]. (The nesting means you will have transpose = [ [list comprehension statement] for value in list]
Exercise 6: Write a lambda function gg that returns the cube of the argument you give it.
Exercise 7: The filter(function, list) function in Python takes in a function and a list as arguments. This offers an nice way to filter out all the elements of a sequence for which the function returns True. Use the filter function and a lambda expression to filter out the even numbers in the list
a_list = [2, 8, 5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
Recall that if you want to return a list you can wrap the expresssion in list() just as you would to turn something into an integer with int().
Exercise 8: Define two functions additionaddition and multiplicationmultiplication that take in one value xx and add it to itself, and multiply it by itself. Make a list of these functions. Then, use list comprehension along with map( ) and a lambda expression inside to make a list of elements which are the the functions applied to the values in range 1 to 30. (Hint: Use a for loop to do this with map( ) first, and then figure out how to use list comprehension instead.)
This assignment has been answered 4 times in private sessions.
© 2023 Codify Tutor. All rights reserved