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












1















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










share|improve this question

























  • I think you just need to write placenum = list(enumerate(sorted(place)))

    – fafl
    Nov 21 '18 at 13:57











  • Why are you defining enumerate when there is a builtin of that same name and definition?

    – Fred Larson
    Nov 21 '18 at 13:58











  • you are creating a generator, and then printing it. what did you expect?

    – juanpa.arrivillaga
    Nov 21 '18 at 13:58






  • 1





    Also, there already is a builtin enumerate

    – juanpa.arrivillaga
    Nov 21 '18 at 13:59
















1















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










share|improve this question

























  • I think you just need to write placenum = list(enumerate(sorted(place)))

    – fafl
    Nov 21 '18 at 13:57











  • Why are you defining enumerate when there is a builtin of that same name and definition?

    – Fred Larson
    Nov 21 '18 at 13:58











  • you are creating a generator, and then printing it. what did you expect?

    – juanpa.arrivillaga
    Nov 21 '18 at 13:58






  • 1





    Also, there already is a builtin enumerate

    – juanpa.arrivillaga
    Nov 21 '18 at 13:59














1












1








1








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










share|improve this question
















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







python python-3.x python-2.7 sorting enumerate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:58







Shandie Tucker

















asked Nov 21 '18 at 13:53









Shandie TuckerShandie Tucker

203




203













  • I think you just need to write placenum = list(enumerate(sorted(place)))

    – fafl
    Nov 21 '18 at 13:57











  • Why are you defining enumerate when there is a builtin of that same name and definition?

    – Fred Larson
    Nov 21 '18 at 13:58











  • you are creating a generator, and then printing it. what did you expect?

    – juanpa.arrivillaga
    Nov 21 '18 at 13:58






  • 1





    Also, there already is a builtin enumerate

    – juanpa.arrivillaga
    Nov 21 '18 at 13:59



















  • I think you just need to write placenum = list(enumerate(sorted(place)))

    – fafl
    Nov 21 '18 at 13:57











  • Why are you defining enumerate when there is a builtin of that same name and definition?

    – Fred Larson
    Nov 21 '18 at 13:58











  • you are creating a generator, and then printing it. what did you expect?

    – juanpa.arrivillaga
    Nov 21 '18 at 13:58






  • 1





    Also, there already is a builtin enumerate

    – juanpa.arrivillaga
    Nov 21 '18 at 13:59

















I think you just need to write placenum = list(enumerate(sorted(place)))

– fafl
Nov 21 '18 at 13:57





I think you just need to write placenum = list(enumerate(sorted(place)))

– fafl
Nov 21 '18 at 13:57













Why are you defining enumerate when there is a builtin of that same name and definition?

– Fred Larson
Nov 21 '18 at 13:58





Why are you defining enumerate when there is a builtin of that same name and definition?

– Fred Larson
Nov 21 '18 at 13:58













you are creating a generator, and then printing it. what did you expect?

– juanpa.arrivillaga
Nov 21 '18 at 13:58





you are creating a generator, and then printing it. what did you expect?

– juanpa.arrivillaga
Nov 21 '18 at 13:58




1




1





Also, there already is a builtin enumerate

– juanpa.arrivillaga
Nov 21 '18 at 13:59





Also, there already is a builtin enumerate

– juanpa.arrivillaga
Nov 21 '18 at 13:59












1 Answer
1






active

oldest

votes


















2














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. E.g.



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





share|improve this answer
























  • I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

    – Shandie Tucker
    Nov 21 '18 at 14:37











  • That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

    – alexis
    Nov 21 '18 at 15:02













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413625%2fhow-to-enumerate-and-sort-a-list-from-a-text-document-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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. E.g.



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





share|improve this answer
























  • I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

    – Shandie Tucker
    Nov 21 '18 at 14:37











  • That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

    – alexis
    Nov 21 '18 at 15:02


















2














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. E.g.



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





share|improve this answer
























  • I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

    – Shandie Tucker
    Nov 21 '18 at 14:37











  • That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

    – alexis
    Nov 21 '18 at 15:02
















2












2








2







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. E.g.



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





share|improve this answer













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. E.g.



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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 13:57









alexisalexis

33.9k956115




33.9k956115













  • I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

    – Shandie Tucker
    Nov 21 '18 at 14:37











  • That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

    – alexis
    Nov 21 '18 at 15:02





















  • I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

    – Shandie Tucker
    Nov 21 '18 at 14:37











  • That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

    – alexis
    Nov 21 '18 at 15:02



















I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

– Shandie Tucker
Nov 21 '18 at 14:37





I can get it to enumerate the sorted time now but it now prints out all the line instead of just the enumeration and time. for rank, value in enumerate(sorted(myList, key=lambda kv: kv[3]),1): print(rank, value)

– Shandie Tucker
Nov 21 '18 at 14:37













That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

– alexis
Nov 21 '18 at 15:02







That's right, adjust it any way you want. Your question was about the use of enumerate. Note that if you enumerate tuples, you get two levels of nesting in the result. So you could write for rank, (num, last, org, time) in enumerate(myList): ....

– alexis
Nov 21 '18 at 15:02






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413625%2fhow-to-enumerate-and-sort-a-list-from-a-text-document-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory