Created
July 31, 2017 19:06
-
-
Save jgarciabu/e98f7e70055e6d4242eea83b51f8c5d9 to your computer and use it in GitHub Desktop.
Ex 10 Diff Size List Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Author: Jeff Garcia | |
This week’s exercise is going to be revisiting an old exercise | |
(see Exercise 5), except require the solution in a different way. | |
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. Write this in one line of Python using at | |
least one list comprehension. (Hint: Remember list comprehensions from Exercise | |
7). | |
The original formulation of this exercise said to write the solution using one | |
line of Python, but a few readers pointed out that this was impossible to do | |
without using sets that I had not yet discussed on the blog, so you can either | |
choose to use the original directive and read about the set command in Python | |
3.3, or try to implement this on your own and use at least one list | |
comprehension in the solution. | |
Extra: | |
Randomly generate two lists to test this | |
""" | |
import random | |
a = random.sample(range(30), 12) | |
b = random.sample(range(30), 20) | |
print (a) | |
print(b) | |
print(list(set([item for item in a if item in b]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment