GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Beginner-Python-exercises/Exercise-05-List-Overlap.py at master · ihossai4/Beginner-Python-exercises Expand file tree/ Copy pathExercise-05-List-Overlap.pyMore file actions 27 lines (22 loc) · 848 Bytes / Copy pathExercise-05-List-Overlap.pyFile metadata and controls27 lines (22 loc) · 848 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ''' Exercise 5: List Overlap Take two lists, say for example these two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Extras: Randomly generate two lists to test this Write this in one line of Python (don’t worry if you can’t figure this out at this point - we’ll get to it soon) ''' import random # Solution # a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] # b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] a = [int(10 * random.random()) for i in range(20)] b = [int(10 * random.random()) for i in range(15)] print("List 1: ", a) print("List 2: ", b) print("Overlap: ", list(set(a).intersection(b))) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |