list - Python Sorting and Organising -
i'm trying sort data file , not quiet getting need. have text file race details ( name placement( ie 1,2,3). able organize data highest placement first , alphabetically name. can if split lines name , score not match up.
any , suggestion welcomed, i've hit proverbial wall.
my apologies ( first time user site , , python noob, steep learning curve ) thank suggestions , appreciate help.
comp=[] results = open('d:\\test.txt', 'r') line in results: line=line.split() # (name,score)= line.split() comp.append(line) sorted(comp) results.close() print (comp) test file in format:
jones 2
ranfel 7
peterson 5
smith 1
simons 9
roberts 4
mcdonald 3
rogers 6
elliks 8
helm 10
i agree has down-voted question being badly posed. however, i'm in mood i'll try , @ least steer in right direction:
let's assume text file looks this:
name,placement d,1 d,2 c,1 c,3 b,1 b,3 a,1 a,4 i suggest importing data , sorting using pandas http://pandas.pydata.org/
import pandas pd # read in data # replace <full_path_of file> c:/data/racedetails.csv # first row automatically used column names data=pd.read_csv("<full_path_of_file>") # sort data sorted_data=data.sort(['placement','name']) # create re-indexed data frame if desire sorted_data_new_index=sorted_data.reset_index(drop=true) this gives me:
name placement 1 b 1 c 1 d 1 d 2 b 3 c 3 4 i'll leave figure out rest..
Comments
Post a Comment