Last active
October 4, 2020 20:17
-
-
Save Manojbhat09/0d56dfe5a20cc4b50c8904d956771feb to your computer and use it in GitHub Desktop.
Multiple Nested loops:: Fast nested loop function to use with your data processing (with variable list input).
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
'''Intro | |
If you have "Multiple Nested loops" to operate on a list with O(n^k) complexity, | |
Save time complex on itermediate list operations by utilizing python map-reduce! | |
''' | |
import numpy as np | |
from itertools import product | |
import time | |
'''Proposed | |
Nested loop maper | |
''' | |
def quick_nested_loop(fn, *args): | |
try: | |
maper = map(lambda *x: fn(*x), product(*args[0])) | |
except Exception as e: | |
print(e) # Do something | |
maper = None | |
return maper | |
''' | |
Example Function to apply any operations on multiple (n-) lists | |
''' | |
def export(args): | |
one, two, three = args | |
return str(one)+str(two)+str(three) | |
#--------------------------------------- | |
# TESTS | |
if __name__ == "__main__": | |
ls1 = np.arange(100).tolist() | |
ls2 = np.arange(100).tolist() | |
ls3 = np.arange(1000).tolist() | |
''' | |
Using function with 3 nested for loop | |
''' | |
strt_time = time.time() | |
result_list=[] | |
for i in ls1: | |
for j in ls2: | |
for k in ls3: | |
result_list.append(export([i,j,k])) | |
print("Finished time - O(n^3) loop: ", time.time() - strt_time) | |
# Finished time - O(n^3) loop: 11.05464506149292 | |
''' | |
Function with map-based nested loop | |
''' | |
strt_time = time.time() | |
maper = quick_nested_loop(export, [ls1, ls2, ls3]) | |
result_list = map(list, [maper]) | |
print("Finished time - quick nesting loop: ", time.time() - strt_time) | |
# Finished time - quick nesting loop: 0.4097409248352051 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment