Search code examples
pythonmatrixiterable-unpacking

Mapping tuple (R, R) into ((R,R),R)?


Input

[[0 0 0 0 0]
 [0 4 0 0 0]
 [0 1 0 0 0]
 [0 1 2 0 0]
 [0 1 2 3 0]]

Intended output

[[(0, day00) (0, day01) (0, day02) (0, day03) (0, day04)]
 [(0, day10) (4, day11) (0, day12) (0, day13) (0, day14)]
 [(0, day20) (1, day21) (0, day22) (0, day23) (0, day24)]
 [(0, day30) (1, day31) (2, day32) (0, day33) (0, day34)]
 [(0, day40) (1, day41) (2, day42) (3, day43) (0, day44)]]

Related

  1. Here a function to generate random days with proportion.
  2. Here a function to generate the random valuation matrix

Solution

  • Scan the source matrix and generate a result matrix, one-to-one:

    random_matrix = generate_random_matrix(...) # the way you want
    result = []
    for row in random_matrix:
      result_row = []
      for value in row:
        result_row.append((value, randomDate(...)))
      result.append(result_row)
    print result # or whatever
    

    A shorter but more cryptic way would be using comprehensions:

    result = [ 
      [(value, randomDate(...)) for value in row] 
      for row in genenerate_random_matrix(...) 
    ]