-2

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
Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282
  • What does the dollar sign mean? What does `day01` mean? what are you trying to do? – recursive Oct 04 '11 at 21:54
  • @recursive: To find the functino $V$ so that $V: (\mathbb R, \mathbb R) \mapstop ((\mathbb R, \mathbb R), \mathbb R)$, with the above constraints. In plain english, I have valuation matrix $A$ and I want that each value $v_{ij} \in A$is associated with random date in the matrix. Simpler way to do this is to create a date matrix of the same size as $A$. Any easy way to unpack the matrix and associate it with the dates? – hhh Oct 04 '11 at 22:01
  • 1
    @hhh: You aren't speaking Python. Your input is not valid Python. And dollar signs mean nothing in Python. I don't know what you mean by them. And what is "$V: (\mathbb R, \mathbb R) \mapstop ((\mathbb R, \mathbb R), \mathbb R)$"??? – Steven Rumbalski Oct 04 '11 at 22:26
  • "Valuation matrix $A$" is plain english?? I still have no idea what those dollar signs mean. – recursive Oct 04 '11 at 23:48
  • $(R,R)$ is an Euclidean space in 2D. $((R,R), R)$ is some different space. Ask in Math.SE if you cannot understand them. – hhh Oct 05 '11 at 00:04

3 Answers3

1

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(...) 
]
9000
  • 39,899
  • 9
  • 66
  • 104
  • ...but I am still wondering whether we are reinventing the wheel? Perhaps, numpy has this...have to keep searching...some product... – hhh Oct 14 '11 at 13:52
0
def o(inputt):
    output = []
    for i, arr in enumerate(inputt):
        n = []
        for j, x in enumerate(arr):
            n.append((x, "day" + str(i) + str(j)))
        output.append(n)
    return output

print o([[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]])
amit kumar
  • 20,438
  • 23
  • 90
  • 126
0
x = [[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,],]

enum = enumerate
[[(item, 'day%02d' % (i*10+j)) for j, item in enum(row)] for i, row in enum(x)]

gives

[[(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')]]
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119