I have a csv file that contains election data from 1976-2018. There are 5 rows(year, state, district, party, and votes)there multiple entries for every year. I want to find the smallest number of votes from the year 1976 to 2018. How can I code that using selection sort but in beginner code?
CodePudding user response:
import csv
# open the csv file
with open(filename, 'r') as f:
# read the csv file into list of lists - ignore headers
rows = [*csv.reader(f)][1:]
# get the min row based on the last element on every row (votes)
min_row = min(rows, key=lambda x:x[-1])
# print the row
print(min_row)
CodePudding user response:
Not using selection sort, but easiest solution to find smallest:
import csv
filename = 'file.csv'
minimum = 9999999999
minimum_row = array()
with open(filename, 'r') as csvfile:
datareader = csv.reader(csvfile)
for row in datareader:
if row[0] >= 1976 and row[0] >= 2018:
if row[3] < minimum:
minimum=row[3]
minimum_row=row
print("Minimum is: %d" % minimum)