-1

How can i get the expected result? Note i dont know the length of first list. Every inner list inside the main list has the same length(this case its 3).

arr = [[1, 'ok', 3.1],
   [4, 'done', 4.5],
   [3, 'hola', 0.3],
   [23, 'mew', 45.7]]

Expected Output :

# [[1, 'ok', 3.1], 
# [[1, 'ok', 4.5],
# [1, 'ok', 0.3],
# [1, 'ok', 45.7],
# [1, 'done', 3.1],
# [1, 'done', 0.3],
# [1, 'done', 45.7],
# [1, 'hola', 3.1],
# [1, 'hola', 4.5],
# [1, 'hola', 45.7],
# ...........
# ...........
# [[3, 'ok', 3.1],
# [[3, 'ok', 4.5],
# [3, 'ok', 0.3],
# [3, 'ok', 45.7],
# SO ON
That's Enam
  • 286
  • 2
  • 3
  • 16
  • use pprint? https://docs.python.org/3/library/pprint.html?highlight=pprint#pprint.pprint – bb1950328 Jun 11 '20 at 06:36
  • 1
    Does this answer your question? [Get the cartesian product of a series of lists?](https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) – azro Jun 11 '20 at 06:37

1 Answers1

1

You may combine product and zip

  • zip is make lists of values at the same indexes : [1,4,3,23], ['ok', 'done', ..

  • product is to make a cartesian product between all

    product([1,2], ['a', 'b']) => [1,'a'], [1,'b'], [2,'a'], [2,'b']
    
from itertools import product
arr = [[1, 'ok', 3.1],
   [4, 'done', 4.5],
   [3, 'hola', 0.3],
   [23, 'mew', 45.7]]

res = product(*zip(*arr))

for a in res:
  print(a)
azro
  • 53,056
  • 7
  • 34
  • 70
  • @That'sEnam You may now think about [accepting an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – azro Jun 11 '20 at 14:19