#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Step 5 ''' from __future__ import print_function from base import create_csv, TARGET_DIR, MODEL_LIST import os import csv def get_sorted_list(model_name): _m = model_name result = [] fpath = os.path.join(TARGET_DIR, 'part4', '%s-step4.csv' % _m) if _m not in MODEL_LIST: return [] weight_index = MODEL_LIST.index(_m) + 5 # offset with open(fpath, 'rb') as f: rows = csv.reader(f) for r in rows: # result[r[0]] = [r[1], r[2], r[3], r[4], r[5], r[6]] result.append([ r[0], # no of tree r[1], # lo_id r[2], # ??? r[3], # ?? r[4], # something count r[weight_index] ]) sorted_result = sorted(result, key=lambda x: x[5], reverse=True) return sorted_result def loop_thru_model(): result = {} max_count = 0 for m in MODEL_LIST: result[m] = get_sorted_list(m) max_count = max_count if max_count > len(result[m]) else len(result[m]) return (result, max_count) sorted_by_weight, max_count = loop_thru_model() rows = [] # fill up header since there is no way to know what model is row = ['|', ] for m in MODEL_LIST: row += ['-', '-', m, '-', '-', '|'] rows.append(row) for i in xrange(0, max_count): row = [(i+1), ] for m in MODEL_LIST: try: row += sorted_by_weight[m][i] except IndexError: row += ['', '', '', '', '', ''] rows.append(row) create_csv('sorted_all.csv', rows, directory='part5')