Write a function assoc_list(l) that, given a list 1 , returns a list of tuples where the first integer represents the element of the int list i and the second integer represents the number of occurrences of that element in the list. This associative list should not contain duplicates. Order does not matter. For example, assoc_list [1, 2, 2, 1, 3] [(2, 2), (1, 2), (3, 1)] . We recommend you to use the reduce function for this. (We had this problem before in Assignment 2.) from functools import reduce def assoc_list (1): # YOUR CODE HERE result = assoc_list [1; 2; 2; 1; 3] result.sort(key=lambda x:x[0]) # sort the result list by the first element of a tuple. assert (result [(1,2), (2, 2), (3, 1)]). ==

Respuesta :

To write a function assoc_list(l) that, given a list 1 , returns a list of tuples where the first integer represents the element of the int list i and the second integer represents the number of occurrences of that element in the list check the code given below

What is an element?

An end tag, some content, and a start tag make up an HTML element.

The start tag and the end tag together make up the HTML element.

<tagname> Content here...</tagname>

Elements in HTML can be nested (this means that elements can contain other elements).

Nested HTML elements are present in every HTML document.

Four HTML elements are used in the example below (html, body, h1, and p):

↓↓//CODE//↓↓

from functools import reduce

def assoc_list(l):

   # YOUR CODE HERE

   # using reduce to count all occurance of each and every element

   cnt = [reduce(lambda x, y: x + (1 if y == p else 0), l, 0) for p in l]

   # zipping l and cnt then making a set

   # to delete repeting values

   # then turning the set to a list

   return list(set(zip(l,cnt)))

result = assoc_list([1, 2, 2, 1, 3])

result.sort(key=lambda x:x[0]) # sort the result by the first element of a tuple

assert(result == [(1, 2), (2, 2), (3, 1)])

Learn more about element

https://brainly.com/question/11569274

#SPJ4