开发者

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


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(...) 
]


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]])


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')]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜