Question: Problem 5. List Comprehensions Write your answers in a Python file p5.py and paste it to the h2.doc/pdf file. a) Assume a variable numbers refers

 Problem 5. List Comprehensions Write your answers in a Python file

p5.py and paste it to the h2.doc/pdf file. a) Assume a variable

Problem 5. List Comprehensions Write your answers in a Python file p5.py and paste it to the h2.doc/pdf file. a) Assume a variable numbers refers to a list of numbers, e.g. [4, -3, 0, 2, -1,5]. Write a list comprehension that generates a list of strings 'y*y-...', where the...stand for expression x* x, where x is in list numbers. For our example the generated list is ['y*y-16', y*y-9', y*y-0', 'y*y-4', 'y*y-1', y y-25] b) Assume a variable numbers refers to a list of numbers, e.g. [4, -3, 0, 2, -1,5]. Write a list comprehension that generates a list of strings 'solution #X=Y, where X stand for an index in the list starting with 1 for the first element (i.e. 1,..., len(numbers) and Y stands for the square of the corresponding element from the list For our example, the result should look like: [solution #1-16, 'solution #2-9, 'solution #3-0, solution #4-4', 'solution #5-1', 'solution #6-25] c) Consider a list called Ist. Write a list comprehension the generates a new list of tuples (i, x) where i is an index in the list (starting from 0) and x is the element in Ist at index i. For example if lst=["zero", "one", "two", "three"], the generated list should be [(0, 'zero'), (1, 'one'), (2, 'two), (3, 'three')] d) Suppose lists Ist1 and Ist2 are defined. Write a list comprehension the generates the Cartesian product of lst1 and 1st 2, i.e. new list with tuples (x, y) where x is from Ist1 and y is from Ist2. For example, if Istila, b', 'c'] and Ist2=[ 1, 21, then the generated list should be [Ca, 1), ('a, 2), ('b, 1), ('b

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!