Created
September 21, 2019 12:55
-
-
Save straussmaximilian/3cb24a1f427c13772089c81b0b94bfd7 to your computer and use it in GitHub Desktop.
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
# List comprehension | |
def list_comprehension(tuple_list): | |
""" | |
Takes a list of tuples and isolates all points that are within [0.2,0.4] | |
for the first dimension and between [0.4,0.6] for the second dimension | |
using a list comprehension. | |
""" | |
filtered_list = [_ for _ in tuple_list if (_[0] >= 0.2) and (_[1] >= 0.4) and (_[0] <= 0.4) and (_[1] <= 0.6)] | |
return filtered_list | |
print('List comprehension:\t', end='') | |
%timeit list_comprehension(python_list) | |
# Filter method | |
def filter_fctn(_): | |
""" | |
Takes a tuple and returns True if the first value is within [0.2,0.4] | |
while the second value is between [0.4,0.6]. | |
""" | |
return (_[0] >= 0.2) and (_[1] >= 0.4) and (_[0] <= 0.4) and (_[1] <= 0.6) | |
print('Filter:\t\t\t', end='') | |
%timeit list(filter(filter_fctn, python_list)) | |
def map_fctn(_): | |
""" | |
Takes a tuple and returns it if the first value is within [0.2,0.4] | |
while the second value is between [0.4,0.6]. | |
""" | |
if (_[0] >= 0.2) and (_[1] >= 0.4) and (_[0] <= 0.4) and (_[1] <= 0.6): | |
return _ | |
print('Map:\t\t\t', end='') | |
%timeit list(filter(map_fctn, python_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment