简体   繁体   中英

How to enumerate and sort a list from a text document in Python

I am trying to enumerate a list in python using a text document. I have read-in the document but can't seem to sort or enumerate them. The goal is to call in the time that they finished the race, sort them, and then enumerate them by order of finish.

The output is always: (['18:44'], generator object enumerate at 0x0000000003143630) I am not sure why it is saying "generator object enumerate at 0x0000000003143630" or how to enumerate the sorted list.

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))

The first "for line in myList" is just a test to see how it works on its own. I will eventually place it in the second "for line in myList" section to clean up the code a bit more. Thanks in advance for your help.

THIS IS THE CURRENT OUTPUT

(['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>)

Process finished with exit code 0

enumerate does not return a list, but a special kind of generator. Write

placenum = list(enumerate(sorted(place)))

and your program's behavior will make more sense. Alternately you can use (consume) a call to enumerate() directly in a for loop, as if it was a list. Eg

for rank, value in enumerate(sorted(place)):
    print(rank, value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM