Created
November 29, 2021 21:33
-
-
Save alexhallam/26e6e4ef3e2e97fe0740711121da6f86 to your computer and use it in GitHub Desktop.
use tuple products and parallel map.py
This file contains hidden or 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
import numpy as np | |
from itertools import product | |
from multiprocessing import Pool | |
def smash(a_tuple): | |
""" | |
smash some strings and numbers together and return final string | |
""" | |
return a_tuple[0] + str(a_tuple[1]) + a_tuple[2] + str(a_tuple[3]) | |
location_string = "location_" | |
location_list = np.arange(0, 3) | |
item_string = "_product_" | |
item_list = np.arange(0, 3) | |
product_location_X_item = list( | |
product([location_string], location_list, [item_string], item_list) | |
) | |
# prod = list(map(smash, product_location_X_item)) | |
# print(prod) | |
# easy conversion to parallel | |
with Pool() as P: | |
parallel_prod = list(P.map(smash, product_location_X_item)) | |
print(parallel_prod) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment