Creating a 2D Array from Another 2D Array (Python) -
hi working 2d array formatted list of lists (here little sample unformatted):
[[10017.0, 93454.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.6, 0.0], [10024.0, 39374.0, 12.8, 28.8, 24.0, 9.829, 28.8, 62.4, 26.4, 30.4, 18.8, 4.0, 20.0, 16.0], [10024.0, 62807.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 38.0, 25.6, 70.4], [10024.0, 81405.0, 11.2, 12.0, 13.6, 7.2, 46.8, 25.6, 22.4, 8.0, 3.2, 12.0, 16.0, 27.2], [10026.0, 2964.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.717, 9.867, 11.683, 0.0, 5.417, 14.316], [10026.0, 29100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.608, 1.531, 3.136], [10026.0, 39461.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 16.0, 0.0, 2.4, 0.0], [10026.0, 42209.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 16.8, 8.0, 2.4] ...
the formatted version appears this:
12230.000 116815.000 17.200 8.000 10.000 9.000 6.000 11.600 6.000 12230.000 132820.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 12366.000 93769.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 12366.000 93961.000 6.400 20.000 0.000 0.000 12.000 4.000 16.000 12369.000 40256.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 12369.000 48122.000 37.600 43.200 48.000 45.600 52.000 53.600 40.000 12379.000 22775.000 0.000 0.000 0.000 0.000 0.000 0.000 37.983 (some of columns deleted formatting purposes). array displays section id in first column, employee id in second, , 12 columns each month indicating how many hours each employee worked.
i need design function looks @ each employee row , calculates cumulative hours worked employees in each department. in case of illustration data, have row dep id. 12230, 12366, 12369, , 12379 followed 12 columns each month. function output info in 2d array in same format list of lists. function have below works in find cumulative hours worked each department, each month. however, not display section id , prints info individual lists. input on how can modify code appreciated!
def cost_center_sum (a): dep_list = [] row in range(0,len(a)): dep_list.append(a[row][0]) mylist = sorted(set(dep_list)) in range(0,len(mylist)): sum_list = [] col in range(2, len(a[0])): sum = 0 row in range(0, len(a)): if mylist[i]==a[row][0]: sum = sum + a[row][col] sum_list.append(sum) print sum_list
try :
number_of_months = 12 def collapse(a): dep = dict() row in a: if row[0] not in dep: dep[row[0]] = [0.0 x in xrange(number_of_months)] in xrange(number_of_months): dep[row[0]][i] += row[i+2] ret = list() section in dep: x = [department] x.extend(dep[department]) ret.append(x) homecoming ret running test data, output :
[10024.0, 24.0, 40.8, 37.6, 17.029, 75.6, 88.0, 48.8, 38.4, 22.0, 54.0, 61.6, 113.60000000000001] [10017.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.6, 0.0] [10026.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.717, 9.867, 27.683, 18.408, 17.348, 19.852]] python arrays
No comments:
Post a Comment