遇到代码问题。给出了一个名为“racing.csv”的文件,该文件存储了“Drive”类中的变量。问题背后的概念是程序应该对比赛时间(从最低到最高)进行排序,并为前三名选手分配点,然后将此数据导出到新文件。除了我在驱动器上调用shortBubbleSort并且没有正确排序竞争对手时,所有代码都正常工作。感谢帮助。
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)
答案 0 :(得分:0)
这有帮助吗?
**范围函数sintax *:def shortBubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
start:序列的起始编号。
停止:生成最多但不包括此数字的数字。
步骤:序列中每个数字之间的差异。
extension=php_openssl.dll