dontworry48
26.03.2020 •
Computers and Technology
Write a function list_intersection that takes two lists as parameters. Return a list that gives the intersection of the two lists- ie, a list of elements that are common to both lists. Run the following test cases and make sure your results are the same (the ordering of your outputs does not matter - [3,2] is the same as [2,3]):
Solved
Show answers
More tips
- C Computers and Internet How to Properly Order Clothing from International Online Stores...
- F Food and Cooking How to Calculate the Gender of Your Child with Blood?...
- S Society and Politics 10 Tips for Boosting Your Self-Esteem...
- C Computers and Internet How to Create a Folder on Your iPhone?...
- G Goods and services How to sew a ribbon: Tips for beginners...
- F Food and Cooking How to Make Mayonnaise at Home? Secrets of Homemade Mayonnaise...
- C Computers and Internet Which Phone is Best for Internet Surfing?...
- F Food and Cooking Everything You Need to Know About Pasta...
- C Computers and Internet How to Choose a Monitor?...
- H Horoscopes, Magic, Divination Where Did Tarot Cards Come From?...
Answers on questions: Computers and Technology
- C Computers and Technology If you guy need help hw join me id: 747-296-3623 pass: 6a47dC...
- C Computers and Technology The AustralianOpen, the firstof the four annual Grand Slam tournaments, begins(usually) in late January. This year, it beganin February.Write a program thatcalculates thetotalprice...
- C Chemistry Diagrammatically, represent the reactionbetween zinc ond hydrochloricacid...
- M Mathematics Which representation illustrates all the possible outcomes of flipping a coin and rolling a number cube with the numbers 1-6 on it? 1, 2, 3, 4, 5, 6 HT 1.6 HT TT 1, 2, 3, 4,...
- C Chemistry What is the difference between system and surrounding in a chemical reaction...
- M Mathematics PLEASE HELP DESPERATE NEED IM FAILONG MATH TYSM IF U HELP...
- M Mathematics What is the position of E on the number line below? Write your answer as a fraction or mixed number. E...
- M Mathematics PLEASE HELP ITS VERY URGENT HIGH POINT AWARD...
- M Mathematics She had a coupon for 15% off. The tax rate was 7.75%. If the subtotal was $35, how much money did Ms. Morel actually pay in total (after the discount and tax)?...
- H History how did americans feel about the vietnam war? explain. (discuss kent state shootings, muhammad ali and dr. martin luther king jr....
Ответ:
Explanation:
The solution code is written in Python.
Firstly, create a function list_intersection that takes two input lists, l1 and l2 (Line 1). Next, create a new list, inter_list, to hold the intersect numbers from the two input lists (Line 2).
Create a two-layer for loop to traverse through every number in l1 and compare it with every number in l2 (Line 4 - 5). If any one of the numbers from l2 is equal to the current number from L1, add this common number to inter_list (Line 8). This is important to check the current common number is not in the inter_list (Line 7) to avoid number duplication.
We can test the function using the two sample lists (Line 12 -13) and print the result (Line 14). We shall get the output [7, 10].
Ответ:
def list_intersection(list1, list2):
intersected_list = [number for number in list1 if number in list2]
return intersected_list
list1 = [11, 5, 676, 0, 42, 32]
list2 = [0, 89, 211, 11]
print(list_intersection(list1, list2))
Explanation:
- Inside the function, create a new list called intersected_list to check if any number in list1 is also in the list2. If a match is found put the number in the intersected_list. * Take a look how Python enables us to do this with just one line of code
- Then create two lists to compare
- Call the function and print the result
Ответ: