我正在努力克服Python中的问题/任务,并且我非常想法。我需要从一个文件中读取两行,对这些行中的一行(从用户输入的数据中确定的多个文件中排序)进行排序,但在俱乐部运行的情况下返回这两个数据。用户的平均每小时英里数将在几周后计算并存储在一个.txt文件中,并与存储在程序开头的用户ID一起存储,程序的最后一部分将需要读取这些文件(用户ID和平均每小时里程数),并按照每小时的平均里程数进行排序,同时保留用户标识(将两者一起返回以允许汇总)。然后我需要说出排名前几位的选手。任何帮助将不胜感激,我还没有使用SQL等,只是逐行,标准的Python。我的代码没有进行优化,但现在我处于最佳状态。另外,我的朋友建议我使用元组,但我不知道从哪里开始诚实。请原谅任何'基本错误'。我也玩过元组,但从来没有把它们妥善地整合起来,因为我不知道从哪里开始。同时也发现将变量保存为与操作符的冲突问题,这意味着如果不定义每个人和每个人,就不能全局化它们。Python列表/元组在排序方面
def retrieve():
global datasave,y ###don't know where to go from this as it does not work
y=1
if y>3: #just practiced with 2 'users'
y=str(y)
print("All members entered and saved, a comparision of average miles per hour will be intiated")
file=open(y+".txt",'r') #saves files in which they occur for easy 'read'
datasave=file.readline(5)
datasave,y=datasave #gave me a 'cannot assign to operator' error
y=int(y)
y=y+1
else:
avmphlist=[datasave1,datasave2,datasave3,datasave4,datasave5,datasave6,datasave7,datasave8,datasave9,datasave10]
sorted(avmphlist)
print(avmphlist)
print("Unfortunately ",avmphlist[9]," and ",avmphlist[10],"have not made the team, thank you for your participation")
print("Congratulations to ",avmphlist[1],", ",avmphlist[2],", ",avmphlist[3],", ",avmphlist[4],", ",avmphlist[5],", ",avmphlist[6],", ",avmphlist[7]," and ",avmphlist[8],)
你的标题是非常含糊和误导。 – 2015-03-31 16:49:14
整个问题非常模糊......你的代码在哪里?你有什么尝试?什么没有工作? – lodo 2015-03-31 16:51:06