How to find specific value using generator in python












0















I created a generator that returns a tuple, for instance the name of an item and the price.



output = ('apple',$2)


I want to find the tuple when the price is the maximum, out of all outputs of the generator.



I know how to get the maximum of the second item when the tuple is not an output of the generator, but I don't know when the generator needs to be looped.



Should I append the looped outcomes and create a super list first? For instance, [('apple',$2),('pear',$1) ... ]










share|improve this question

























  • $2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

    – Patrick Artner
    Nov 13 '18 at 17:48


















0















I created a generator that returns a tuple, for instance the name of an item and the price.



output = ('apple',$2)


I want to find the tuple when the price is the maximum, out of all outputs of the generator.



I know how to get the maximum of the second item when the tuple is not an output of the generator, but I don't know when the generator needs to be looped.



Should I append the looped outcomes and create a super list first? For instance, [('apple',$2),('pear',$1) ... ]










share|improve this question

























  • $2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

    – Patrick Artner
    Nov 13 '18 at 17:48
















0












0








0








I created a generator that returns a tuple, for instance the name of an item and the price.



output = ('apple',$2)


I want to find the tuple when the price is the maximum, out of all outputs of the generator.



I know how to get the maximum of the second item when the tuple is not an output of the generator, but I don't know when the generator needs to be looped.



Should I append the looped outcomes and create a super list first? For instance, [('apple',$2),('pear',$1) ... ]










share|improve this question
















I created a generator that returns a tuple, for instance the name of an item and the price.



output = ('apple',$2)


I want to find the tuple when the price is the maximum, out of all outputs of the generator.



I know how to get the maximum of the second item when the tuple is not an output of the generator, but I don't know when the generator needs to be looped.



Should I append the looped outcomes and create a super list first? For instance, [('apple',$2),('pear',$1) ... ]







python python-3.x generator iterable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:42









jpp

96.1k2157109




96.1k2157109










asked Nov 13 '18 at 17:36









song0089song0089

87231235




87231235













  • $2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

    – Patrick Artner
    Nov 13 '18 at 17:48





















  • $2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

    – Patrick Artner
    Nov 13 '18 at 17:48



















$2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

– Patrick Artner
Nov 13 '18 at 17:48







$2 is illegal python .. me thinks - either it is a string or a number w/o the $ sign

– Patrick Artner
Nov 13 '18 at 17:48














1 Answer
1






active

oldest

votes


















3














max accepts any iterable. A generator is an iterable. You can supply a custom key function so that max considers only the 1st index:



from operator import itemgetter

# generator expression below can also be a generator function
gen = (i for i in (('apple', 2), ('pear', 1)))

res = max(gen, key=itemgetter(1)) # ('apple', 2)





share|improve this answer


























  • That works if tup[1] is an int, otherwise a custom function to cast as int is needed

    – Dalvenjia
    Nov 13 '18 at 18:01











  • @Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

    – jpp
    Nov 13 '18 at 18:02













  • or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

    – Dalvenjia
    Nov 13 '18 at 18:04











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%2f53286637%2fhow-to-find-specific-value-using-generator-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









3














max accepts any iterable. A generator is an iterable. You can supply a custom key function so that max considers only the 1st index:



from operator import itemgetter

# generator expression below can also be a generator function
gen = (i for i in (('apple', 2), ('pear', 1)))

res = max(gen, key=itemgetter(1)) # ('apple', 2)





share|improve this answer


























  • That works if tup[1] is an int, otherwise a custom function to cast as int is needed

    – Dalvenjia
    Nov 13 '18 at 18:01











  • @Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

    – jpp
    Nov 13 '18 at 18:02













  • or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

    – Dalvenjia
    Nov 13 '18 at 18:04
















3














max accepts any iterable. A generator is an iterable. You can supply a custom key function so that max considers only the 1st index:



from operator import itemgetter

# generator expression below can also be a generator function
gen = (i for i in (('apple', 2), ('pear', 1)))

res = max(gen, key=itemgetter(1)) # ('apple', 2)





share|improve this answer


























  • That works if tup[1] is an int, otherwise a custom function to cast as int is needed

    – Dalvenjia
    Nov 13 '18 at 18:01











  • @Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

    – jpp
    Nov 13 '18 at 18:02













  • or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

    – Dalvenjia
    Nov 13 '18 at 18:04














3












3








3







max accepts any iterable. A generator is an iterable. You can supply a custom key function so that max considers only the 1st index:



from operator import itemgetter

# generator expression below can also be a generator function
gen = (i for i in (('apple', 2), ('pear', 1)))

res = max(gen, key=itemgetter(1)) # ('apple', 2)





share|improve this answer















max accepts any iterable. A generator is an iterable. You can supply a custom key function so that max considers only the 1st index:



from operator import itemgetter

# generator expression below can also be a generator function
gen = (i for i in (('apple', 2), ('pear', 1)))

res = max(gen, key=itemgetter(1)) # ('apple', 2)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 17:48

























answered Nov 13 '18 at 17:40









jppjpp

96.1k2157109




96.1k2157109













  • That works if tup[1] is an int, otherwise a custom function to cast as int is needed

    – Dalvenjia
    Nov 13 '18 at 18:01











  • @Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

    – jpp
    Nov 13 '18 at 18:02













  • or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

    – Dalvenjia
    Nov 13 '18 at 18:04



















  • That works if tup[1] is an int, otherwise a custom function to cast as int is needed

    – Dalvenjia
    Nov 13 '18 at 18:01











  • @Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

    – jpp
    Nov 13 '18 at 18:02













  • or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

    – Dalvenjia
    Nov 13 '18 at 18:04

















That works if tup[1] is an int, otherwise a custom function to cast as int is needed

– Dalvenjia
Nov 13 '18 at 18:01





That works if tup[1] is an int, otherwise a custom function to cast as int is needed

– Dalvenjia
Nov 13 '18 at 18:01













@Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

– jpp
Nov 13 '18 at 18:02







@Dalvenjia, Agreed, in that case, you can use lambda x: int(x[1]) (if it's a string).

– jpp
Nov 13 '18 at 18:02















or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

– Dalvenjia
Nov 13 '18 at 18:04





or lambda x: int(x[1].strip('$')) if the amount is in dollars as stated in the question

– Dalvenjia
Nov 13 '18 at 18:04


















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%2f53286637%2fhow-to-find-specific-value-using-generator-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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python