63.6k views
2 votes
Write a new version (called map2) of the map function which operates on two lists. Your function should accept three parameters, a function f and two lists X and Y , and return a new list composed of the function f applied to corresponding elements of X and Y . For concreteness, place the function first among the parameters, so that a call to the function appears as (map2 f X Y). Your function may assume that the two lists have the same length. In particular, given two lists (x1 x2 . . . xn ) and ( y1 y2 . . . yn ) (and the function f ), map2 should return

User John Joe
by
4.4k points

1 Answer

3 votes

Answer:

def mapper( a, b ):

return dict(zip(a,b))

def map2 ( f, x : list, y: list ) ->dictionary:

mapped_lists = f( x, y )

new_list = [ ]

for key, value in mapped_lists.iteritems():

items = [key,value]

new_list.append(items)

return new_list

result = map2( mapper, x, y )

print( result )

Step-by-step explanation:

The "map2" python function defined above maps two lists of the same length. It receives two iterators or lists and a function and returns a list of mapped items from both lists.

User Nogus
by
4.7k points