Skip to content

Instantly share code, notes, and snippets.

@nnelluri928
Last active September 30, 2021 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnelluri928/a5f850039f8a5a2441fa4cee2ef55a79 to your computer and use it in GitHub Desktop.
Save nnelluri928/a5f850039f8a5a2441fa4cee2ef55a79 to your computer and use it in GitHub Desktop.
itertools module notes

In [30]: num_list = [1, 2, 3]

In [31]: from itertools import combinations

In [32]: comb = combinations(num_list,2)

In [33]: for pair in list(comb): ...: print(pair) ...: (1, 2) (1, 3) (2, 3)

In [47]: from itertools import product

In [48]: params = { ...: "learning_rate": [1e-1, 1e-2, 1e-3], ...: "batch_size": [16, 32, 64], ...: }

In [49]:

In [49]:

In [49]:

In [49]: for vals in product(*params.values()): ...: combination = dict(zip(params.keys(), vals)) ...: print(combination) ...: {'learning_rate': 0.1, 'batch_size': 16} {'learning_rate': 0.1, 'batch_size': 32} {'learning_rate': 0.1, 'batch_size': 64} {'learning_rate': 0.01, 'batch_size': 16} {'learning_rate': 0.01, 'batch_size': 32} {'learning_rate': 0.01, 'batch_size': 64} {'learning_rate': 0.001, 'batch_size': 16} {'learning_rate': 0.001, 'batch_size': 32} {'learning_rate': 0.001, 'batch_size': 64}

In [50]:

In [56]: def multiple(x,y): ...: return x*y ...:

In [57]: nums = [(1, 2), (4, 2), (2, 5)]

In [58]: from itertools import starmap

In [59]: list(starmap(multiple,nums)) Out[59]: [2, 8, 10]

In [60]:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment