Python itertools: Best way to unpack product of product of list of lists

rambalachandran

I have a list of lists over which I need to iterate 3 times (3 nested loops)

rangeList = [[-0.18,0.18],[0.14,0.52],[0.48,0.85]]

I can achieve this using product of product as follows

from itertools import product
for val in product(product(rangeList,rangeList),rangeList):
    print val

The output looks as follows

(([-0.18, 0.18], [-0.18, 0.18]), [-0.18, 0.18])
(([-0.18, 0.18], [-0.18, 0.18]), [0.14, 0.52])
(([-0.18, 0.18], [-0.18, 0.18]), [0.48, 0.85])
(([-0.18, 0.18], [0.14, 0.52]), [-0.18, 0.18])

Its a tuple of tuple. My questions are

  1. Is this a good approach?
  2. If so, what is the bestway to unpack the output of the product val into 3 separate variables say xRange, yRange and zRange, where each holds a list value of say [-0.18, 0.18] or [0.14, 0.52] etc.
shx2

This is probably the most elegant way to do what you want:

for xrange, yrange, zrange in product(rangeList, repeat=3):
    print xrange, yrange, zrange

But just to demonstrate how you can do the "deep" tuple unpacking you were trying:

for (xrange, yrange), zrange in product(product(rangeList,rangeList),rangeList):
    print xrange, yrange, zrange

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the best matching product version from a list of available product versions

From Dev

Python itertools product, but conditional?

From Dev

itertools.product - return list instead of tuple

From Dev

Cartesian Product of a list of lists in Erlang

From Dev

Append itertools.product() results to variable in Python

From Dev

Cartesian product of a list of sets in python

From Dev

What is the best way for me to search this Product database?

From Dev

Minimum of itertools.product in python

From Dev

Python - unpack list of lists

From Dev

efficiency of Python's itertools.product()

From Dev

Cartesian product over a list of lists in Haskell

From Dev

Dot product of two lists in Python

From Dev

Python itertools.product() get item index

From Dev

How to sort itertools.product in Python?

From Dev

Python: faster alternative for itertools.product()?

From Dev

Python's itertools.product in C

From Dev

Fastest way to sample from product of two lists

From Dev

How to dynamically assign arguments to itertools.product in python

From Dev

How to slice process itertools.product in python?

From Dev

Explanation of the Python itertools.product() implementation?

From Dev

Python - itertools.product without using element more than once

From Dev

Cartesian product of two lists in python

From Dev

Find the best matching product version from a list of available product versions

From Dev

How to operate on parameters of python itertools.product

From Dev

generating tuples using n-lists with itertools.product

From Dev

Python itertools product of undefinite dimension

From Dev

Itertools product of list

From Dev

Pythonic way of sum of dot product of two list of lists

From Dev

Best way to design a database for product varients

Related Related

  1. 1

    Find the best matching product version from a list of available product versions

  2. 2

    Python itertools product, but conditional?

  3. 3

    itertools.product - return list instead of tuple

  4. 4

    Cartesian Product of a list of lists in Erlang

  5. 5

    Append itertools.product() results to variable in Python

  6. 6

    Cartesian product of a list of sets in python

  7. 7

    What is the best way for me to search this Product database?

  8. 8

    Minimum of itertools.product in python

  9. 9

    Python - unpack list of lists

  10. 10

    efficiency of Python's itertools.product()

  11. 11

    Cartesian product over a list of lists in Haskell

  12. 12

    Dot product of two lists in Python

  13. 13

    Python itertools.product() get item index

  14. 14

    How to sort itertools.product in Python?

  15. 15

    Python: faster alternative for itertools.product()?

  16. 16

    Python's itertools.product in C

  17. 17

    Fastest way to sample from product of two lists

  18. 18

    How to dynamically assign arguments to itertools.product in python

  19. 19

    How to slice process itertools.product in python?

  20. 20

    Explanation of the Python itertools.product() implementation?

  21. 21

    Python - itertools.product without using element more than once

  22. 22

    Cartesian product of two lists in python

  23. 23

    Find the best matching product version from a list of available product versions

  24. 24

    How to operate on parameters of python itertools.product

  25. 25

    generating tuples using n-lists with itertools.product

  26. 26

    Python itertools product of undefinite dimension

  27. 27

    Itertools product of list

  28. 28

    Pythonic way of sum of dot product of two list of lists

  29. 29

    Best way to design a database for product varients

HotTag

Archive