Question

use python2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list t

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

#defining union function
def union(list1,list2):
    #initializing a new_list
    new_list=[]
    #assigning all elements in list1 to new_list
    new_list+=list1
    #looping through all elements in list2
    for i in list2:
        #checking if that element is not in list1
        if i not in list1:
            #adding that element to new_list
            new_list.append(i)
    #returning new_list
    return new_list
#initializing list a
a=[1,2]
#initializing list b
b=[2,3,4]
#printing their union
print(union(a,b))
#printing list a
print(a)
#printing list b
print(b)

#defining intersection function
def intersection(list1,list2):
    #initializing a new_list
    new_list=[]
    #looping through all elements in list2
    for i in list2:
        #checking if that element is in list1
        if i in list1:
            #adding that element to new_list
            new_list.append(i)
    #returning new_list
    return new_list
#initializing list a
a=[1,2]
#initializing list b
b=[2,3,4]
#printing their intersection
print(intersection(a,b))
#printing list a
print(a)
#printing list b
print(b)

Screenshot:

1 #defining union function 2. def union(listi,list2): 3 #initializing a new_list 4 new_list=[] 5 #assigning all elements in l

38 #initializing list a 39 a=[1,2] 40 #initializing list b 41 b=[2,3,4] 42 #printing their intersection 43 print(intersection
Output:

[1,2,3,4] [1,2] [2,3,4] [2] [1, 2] [2,3,4]

Add a comment
Know the answer?
Add Answer to:
use python 2a. Union (1 points) Make a union function which takes in 2 lists as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In python 3 and without Union() or Intersection() methods!~ The Jaccard index is a measure of...

    In python 3 and without Union() or Intersection() methods!~ The Jaccard index is a measure of similarity between two sets, computed as the size of the intersection of the sets divided by the size of the union of the sets. In other words, the Jaccard index of two sets A and B can be calculated as: Write a function called jaccard that computes the Jaccard index for two argument lists. You can assume that the lists will contain only integer...

  • ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a...

    ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a value and print a result to the console, to test you must call the function, run it and visually inspect the result for correctness. With functions that return a value, use the print_test function to provide feedback of the test results at the command line. The print_test function is implemented for you at the bottom of this file. RECALL: floating point arithmetic can lose...

  • 1. Use ML patterns to write a function member (e, L) that returns true if e is an element of ist ...

    ML LANGUAGE. 1. Use ML patterns to write a function member (e, L) that returns true if e is an element of ist L. 2. Use ML patterns to write a function less (e, L) that returns the list of all elements of L that are less than e. 3. Use ML patterns to write a function union (S1, S2) that returns the union of sets S1 and S2. We implement sets as unordered lists of elements, without repetitions. Hint:...

  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • In python: ScoreFinder is a function that takes in two lists and a string. The first...

    In python: ScoreFinder is a function that takes in two lists and a string. The first list is a list of strings (player names), the second list is a list of floats (player scores), and the string is a name (player to find). If the player to find exists in the list of player names, then print the name of that player along with their associated score (which is in the second list at the same index). If the player...

  • 1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of...

    1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of size m, where each of the m elements in a sublist are randomly generated integers between 1 and 9. For example, you might obtain l = make_list_of_lists(2,2) >>> print(l) [[1, 4], [5, 7]] Big Hint: You can do this using numpy along the lines of the following code: import numpy >>> l = list(numpy.random.randint(a,b,c)) where you need to sort out what the values of...

  • 1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means:...

    1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means: a.) Changes made to the object within the function persist after the function completes b.) The function receives a copy of the object c.) Python keeps a history of where then object was used d.) Statements in the function can refer to the object by nickname 2. Which of the following is NOT a requirement for a Python function: a.) The function must have...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i;...

    PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i; it is passed two linked lists (ll1 and ll2) as arguments. It returns a reference to the front of a linked list that alternates the LNs from ll1 and ll2, starting with ll1; if either linked list becomes empty, the rest of the LNs come from the other linked list. So, all LNs in ll1 and ll2 appear in the returned result (in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT