Highest salary with each city with csv file in python

Given a files salaries.csv with this structure:

City,Job,Salary
Delhi,Doctors,500
Delhi,Lawyers,400
Delhi,Plumbers,100
London,Doctors,800
London,Lawyers,700
London,Plumbers,300
Tokyo,Doctors,900
Tokyo,Lawyers,800
Tokyo,Plumbers,400
...

 Solution




import csv
fin=open('salaries.csv','rb')
reader = csv.reader(fin)
reader.next()
sorted_salary=sorted(reader, key=lambda t: int( t[2] ), reverse=True)
x=0
for row in sorted_salary:
    if row[0] == "Delhi":
       if x<row[2]:
          x=row[2]
          print (row[0],row[1],x)

for row in sorted_salary:
    if row[0] == "Tokyo":
       if x<row[2]:
          x=row[2]
          print (row[0],row[1],x)

for row in sorted_salary:
    if row[0] == "London":
       if x<row[2]:
          x=row[2]
          print (row[0],row[1],x)


o/p
('Delhi', 'Doctors', '500')
('Tokyo', 'Doctors', '900')

No comments:

Post a Comment