2021年1月6日星期三

Transpose n*3 matrix to n*m matrix

For a web server I am trying to generate a report giving the number of requests with various response codes within some fixed duration, say an hour.

From raw http access logs data I first generated an array of array, where each row has 3 cells: the hour, response code, and the number of requests. From that I then generate the final result. The logic is quite simple, for each row in input,

I have it working, see the small sample, but it does not seem "pythonic" I am wondering if there is a better way to do it. I have beginner skills in Python, and no exposure to various data processing libraries.

Is there a better way to do this? Are there any libraries that can be used to directly generate the final result from the initial raw data?

Also, I am pretty sure transpose is not the right name for this transformation, appreciate if someone can correct me on that as well.

#! /usr/bin/python3    '''  data is an array of array n*3:  h  resp  count  1   200    202  1   201     23  2   200      9  2   201     75  2   404      5    result is an array of n*m:     200  202   404  1   15   23     0  2    9   75     5  '''    def process(data):    result = [[None]]    for inrow in data:      r,c,v = inrow[0], inrow[1], inrow[2]      row = find_row(result, r)      idx = find_column_index(result, c)      row[idx] = v    return result    def find_row(result, r):    row = next((row for row in result[1:] if row[0] == r), None)    if not row:      row = [r]      result.append(row)      for x in result[0][1:]:        row.append(0)    return row    def find_column_index(result, c):    columns = result[0]    idx = next((idx for idx in range(len(columns)) if columns[idx] == c), None)    if not idx:      columns.append(c)      for row in result[1:]:        row.append(0)      idx = len(columns) - 1    return idx    def test():    #import pdb; pdb.set_trace()    arr = [      [1, "200", 15],      [1, "202", 23],      [2, "200", 9],      [2, "202", 75],      [2, "404", 5]      ]    result = process(arr)    print(result)    if __name__ == "__main__":    test()    
https://stackoverflow.com/questions/65606075/transpose-n3-matrix-to-nm-matrix January 07, 2021 at 11:00AM

没有评论:

发表评论