【发布时间】:2018-05-02 10:58:59
【问题描述】:
我想在我当前拥有的列表中对列表进行排序,这不是问题,我相信我已将值保存为数组中的单个索引,但需要 finishTime 作为单独的索引需要按此值对数组进行排序。此代码的上下文是一个简单的运动员比赛数据输入表单。 代码如下
import time
datasets= []
for i in range(1, 3):
print("Inputting Data for Lane", i)
gender = input("Is the athlete male or female ")
athlete = input("What is the athletes name ")
finishTime = input("What was the finishing time ")
dataset = ("Gender =", gender, "|" ,"Athlete =", athlete, "|","Finish time", finishTime)
datasets.append(dataset)
for s in datasets:
time.sleep(1)
print("")
print(*s)
应该是这样的
>Male John, 9
>Female Sarah, 7
如您所见,它们应该是两个不同的索引,以便我可以按末尾的数字对列表中的列表进行排序。
【问题讨论】:
-
你可以使用sorted
-
为什么不对数据集使用字典或命名元组,并为数据集使用这些列表?
-
它需要按finishTime排序但是,我认为每个数组只有一个索引。它需要另一个索引,它是可以对其进行排序的 FinishTime,我在执行此操作时遇到了麻烦。
标签: python arrays python-3.x sorting for-loop