python - How can i get this output in order? -


import xlrd import sys  collections import defaultdict out = defaultdict(list)  workbook = xlrd.open_workbook('dummy input data.xlsx') worksheet = workbook.sheet_by_index(0) headers = worksheet.row(0) result = [] index in range(worksheet.nrows)[1:]:     result.append(worksheet.row(index)[0].value) uniq in set(result):     sum = 0     index in range(worksheet.nrows)[1:]:        if uniq == worksheet.row(index)[0].value:             sum = sum + worksheet.row(index)[4].value     out[uniq] = int(sum) rec in out:     print rec+" "+str(out[rec])  got output this: engineer-2 16 engineer-3 19 engineer-1 11 engineer-4 24 engineer-5 12 

how sort names , numbers, need result in orderly like:

engineer-1 11

engineer-2 16

engineer-3 19

engineer-4 24

engineer-5 12

how result above,it should take starting 1,2,3,4,5... getting output not in order.

you use 2 different data-structures not have sense of order - dictionary set - getting result in order different original order (in existed in excel) should not suprising.

if want order result using name 1-5 , can use sorted() on out.keys() , example -

for rec in sorted(out.keys()):     print rec+" "+str(out[rec]) 

if real intention data in original format , existed in excel , should not create set uniq elements, instead create result list of unique elements using set store seen elements. example -

seen = set() result = [] index in range(worksheet.nrows)[1:]:     e = worksheet.row(index)[0].value     if e not in seen:         seen.add(e)         result.append(e) 

then use that, result list iterate dictionary print values. example -

for rec in result:     print rec+" "+str(out[rec]) 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -