如何从 Python 中的文本文档中枚举和排序列表
How to enumerate and sort a list from a text document in Python
我正在尝试使用文本文档枚举 python 中的列表。我已读入文档,但似乎无法对它们进行排序或枚举。目标是调用他们完成比赛的时间,对它们进行排序,然后按完成顺序枚举它们。
输出总是:
(['18:44'],生成器对象枚举在 0x0000000003143630)
我不确定它为什么说 "generator object enumerate at 0x0000000003143630" 或如何枚举排序列表。
def get_sec(time_str):
h, m = time_str.split(':')
return int(h) * 3600 + int(m) * 60
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
with open("Race_Results_Sample.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
place = []
place.append(time)
placenum = enumerate(sorted(place))
print(place, placenum)
for line in myList:
num, last, org, time = line
new_time = get_sec(time)
mile = round((((new_time/ 3.10686)/60)/60), 3)
mile = str(mile)
print ('{:<20s}{:<5s}{:<5s}{:<7s}{:<10s}'.format(last, num, org, time, mile))
第一个 "for line in myList" 只是一个测试,看看它如何独立运行。我最终会将它放在第二个 "for line in myList" 部分,以进一步清理代码。在此先感谢您的帮助。
这是当前输出
(['18:44'], generator object enumerate at 0x0000000003143630>)
(['18:23'], generator object enumerate at 0x0000000003143678>)
(['18:28'], generator object enumerate at 0x0000000003143630>)
(['18:36'], generator object enumerate at 0x0000000003143678>)
(['19:05'], generator object enumerate at 0x0000000003143630>)
(['19:10'], generator object enumerate at 0x0000000003143678>)
(['18:22'], generator object enumerate at 0x0000000003143630>)
(['18:03'], generator object enumerate at 0x0000000003143678>)
(['18:49'], generator object enumerate at 0x0000000003143630>)
(['19:01'], generator object enumerate at 0x0000000003143678>)
(['18:33'], generator object enumerate at 0x0000000003143630>)
(['18:45'], generator object enumerate at 0x0000000003143678>)
(['18:55'], generator object enumerate at 0x0000000003143630>)
(['18:58'], generator object enumerate at 0x0000000003143678>)
(['18:09'], generator object enumerate at 0x0000000003143630>)
进程已完成,退出代码为 0
enumerate
不是 return 列表,而是一种特殊的生成器。写
placenum = list(enumerate(sorted(place)))
并且您的程序的行为将更有意义。或者,您可以在 for
循环中直接使用(使用)对 enumerate()
的调用,就像它是一个列表一样。例如
for rank, value in enumerate(sorted(place)):
print(rank, value)
我正在尝试使用文本文档枚举 python 中的列表。我已读入文档,但似乎无法对它们进行排序或枚举。目标是调用他们完成比赛的时间,对它们进行排序,然后按完成顺序枚举它们。
输出总是: (['18:44'],生成器对象枚举在 0x0000000003143630) 我不确定它为什么说 "generator object enumerate at 0x0000000003143630" 或如何枚举排序列表。
def get_sec(time_str):
h, m = time_str.split(':')
return int(h) * 3600 + int(m) * 60
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
with open("Race_Results_Sample.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
place = []
place.append(time)
placenum = enumerate(sorted(place))
print(place, placenum)
for line in myList:
num, last, org, time = line
new_time = get_sec(time)
mile = round((((new_time/ 3.10686)/60)/60), 3)
mile = str(mile)
print ('{:<20s}{:<5s}{:<5s}{:<7s}{:<10s}'.format(last, num, org, time, mile))
第一个 "for line in myList" 只是一个测试,看看它如何独立运行。我最终会将它放在第二个 "for line in myList" 部分,以进一步清理代码。在此先感谢您的帮助。
这是当前输出
(['18:44'], generator object enumerate at 0x0000000003143630>)
(['18:23'], generator object enumerate at 0x0000000003143678>)
(['18:28'], generator object enumerate at 0x0000000003143630>)
(['18:36'], generator object enumerate at 0x0000000003143678>)
(['19:05'], generator object enumerate at 0x0000000003143630>)
(['19:10'], generator object enumerate at 0x0000000003143678>)
(['18:22'], generator object enumerate at 0x0000000003143630>)
(['18:03'], generator object enumerate at 0x0000000003143678>)
(['18:49'], generator object enumerate at 0x0000000003143630>)
(['19:01'], generator object enumerate at 0x0000000003143678>)
(['18:33'], generator object enumerate at 0x0000000003143630>)
(['18:45'], generator object enumerate at 0x0000000003143678>)
(['18:55'], generator object enumerate at 0x0000000003143630>)
(['18:58'], generator object enumerate at 0x0000000003143678>)
(['18:09'], generator object enumerate at 0x0000000003143630>)
进程已完成,退出代码为 0
enumerate
不是 return 列表,而是一种特殊的生成器。写
placenum = list(enumerate(sorted(place)))
并且您的程序的行为将更有意义。或者,您可以在 for
循环中直接使用(使用)对 enumerate()
的调用,就像它是一个列表一样。例如
for rank, value in enumerate(sorted(place)):
print(rank, value)