Skip to content

Instantly share code, notes, and snippets.

@straussmaximilian
Created September 21, 2019 12:55
Show Gist options
  • Save straussmaximilian/3cb24a1f427c13772089c81b0b94bfd7 to your computer and use it in GitHub Desktop.
Save straussmaximilian/3cb24a1f427c13772089c81b0b94bfd7 to your computer and use it in GitHub Desktop.
# 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