Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name parts of iterables in itertools.products

I've been reading about itertools, which seems to be a very powerful module. I am particularly interested in itertools.product() which appears to give me all of the combinations of the iterable inputs.

However, I would like to know which of the input iterables each of the outputs are coming from. For example, a simple standard example is:

itertools.product([1, 2, 3], [1, 2])

If the user provided the inputs of [1,2,3], [1, 2] I won't know which order they came in, so getting a result of

(1, 2)

isn't much help, as I don't know which way round they will be. Is there some way of providing input like:

itertools.product(foo = [1, 2, 3], bar = [1, 2])

and then getting outputs like:

output['foo'] = 1
output['bar'] = 2

or

output.foo = 1
output.bar = 2
like image 876
robintw Avatar asked Feb 01 '12 15:02

robintw


People also ask

What Does product do in Itertools?

What does itertools. product() do? itertools. product() is used to find the cartesian product from the given iterator, output is lexicographic ordered.

What is chain () in Python?

chain() function It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output.

Why Itertool is used in Python?

Itertools is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.


1 Answers

The output of itertools.product([1, 2, 3], [1, 2]) is a series of ordered pairs whether the first element comes from [1,2,3] and the second element from [1,2]. This is guaranteed behavior.

If field names are desired, you can cast the result to a named tuple. As you requested, the named tuple lets you access the fields with output.foo and output.bar. Incorporating KennyTM's idea of using **items, it can be packaged in a single function that is fast and memory efficient:

from itertools import product, starmap
from collections import namedtuple

def named_product(**items):
    Product = namedtuple('Product', items.keys())
    return starmap(Product, product(*items.values()))

Here's an example call:

>>> for output in named_product(foo=[1,2,3], bar=[1,2]):
        print output

Product(foo=1, bar=1)
Product(foo=1, bar=2)
Product(foo=2, bar=1)
Product(foo=2, bar=2)
Product(foo=3, bar=1)
Product(foo=3, bar=2)
like image 173
Raymond Hettinger Avatar answered Oct 16 '22 15:10

Raymond Hettinger