0
存在代碼問題。獲得了一個名爲「racing.csv」的文件,該文件存儲了「Drive」類中找到的變量。問題背後的概念是,程序應該對比賽時間(從最低到最高)進行排序,並將點數分配給前3名賽車手,然後將這些數據導出到新文件中。除了在調用驅動器上的shortBubbleSort時,所有代碼都可以正常工作,並且不會正確排列比賽時間。幫助表示讚賞。對csv文件中的列進行排序(Python)
import csv
class Drive(object):
driver = ""
team = ""
racetime = 0.0
points = 0
def __init__(self,driver,team,racetime,points):
self.driver = driver
self.team = team
self.racetime = racetime
self.points = points
f = open('racing.csv', 'r')
csv_f = list(csv.reader(f))
driverclasses = []
for i in range(len(csv_f)):
d = Drive(csv_f[i][0],csv_f[i][1],csv_f[i][2],csv_f[i][3])
driverclasses.append(d)
for row in csv_f:
print (row)
for x in range(0, 6):
csv_f[x][2]=(input("Enter Racetime"))
def shortBubbleSort(alist):
exchanges = True
passnum = len(alist)-1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if alist[i]>alist[i+1]:
exchanges = True
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
passnum = passnum-1
shortBubbleSort(Drive)
print(csv_f)
csv_f[0][3] = 25
csv_f[1][3] = 18
csv_f[2][3] = 15
f = open('RacingResults.csv', 'w')
for row in csv_f:
print (row)
可能沒有,因爲還有很多其他問題:shortBubbleSort(Drive) –
我試過一個簡單的例子,我發佈的解決方案似乎工作。讓我們來看看@Sandy所說的:) –