How to find specific value using generator in python
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
add a comment |
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
$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
add a comment |
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
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
python python-3.x generator iterable
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
add a comment |
$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
add a comment |
1 Answer
1
active
oldest
votes
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)
That works iftup[1]
is anint
, otherwise a custom function to cast as int is needed
– Dalvenjia
Nov 13 '18 at 18:01
@Dalvenjia, Agreed, in that case, you can uselambda x: int(x[1])
(if it's a string).
– jpp
Nov 13 '18 at 18:02
orlambda x: int(x[1].strip('$'))
if the amount is in dollars as stated in the question
– Dalvenjia
Nov 13 '18 at 18:04
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
That works iftup[1]
is anint
, otherwise a custom function to cast as int is needed
– Dalvenjia
Nov 13 '18 at 18:01
@Dalvenjia, Agreed, in that case, you can uselambda x: int(x[1])
(if it's a string).
– jpp
Nov 13 '18 at 18:02
orlambda x: int(x[1].strip('$'))
if the amount is in dollars as stated in the question
– Dalvenjia
Nov 13 '18 at 18:04
add a comment |
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)
That works iftup[1]
is anint
, otherwise a custom function to cast as int is needed
– Dalvenjia
Nov 13 '18 at 18:01
@Dalvenjia, Agreed, in that case, you can uselambda x: int(x[1])
(if it's a string).
– jpp
Nov 13 '18 at 18:02
orlambda x: int(x[1].strip('$'))
if the amount is in dollars as stated in the question
– Dalvenjia
Nov 13 '18 at 18:04
add a comment |
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)
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)
edited Nov 13 '18 at 17:48
answered Nov 13 '18 at 17:40
jppjpp
96.1k2157109
96.1k2157109
That works iftup[1]
is anint
, otherwise a custom function to cast as int is needed
– Dalvenjia
Nov 13 '18 at 18:01
@Dalvenjia, Agreed, in that case, you can uselambda x: int(x[1])
(if it's a string).
– jpp
Nov 13 '18 at 18:02
orlambda x: int(x[1].strip('$'))
if the amount is in dollars as stated in the question
– Dalvenjia
Nov 13 '18 at 18:04
add a comment |
That works iftup[1]
is anint
, otherwise a custom function to cast as int is needed
– Dalvenjia
Nov 13 '18 at 18:01
@Dalvenjia, Agreed, in that case, you can uselambda x: int(x[1])
(if it's a string).
– jpp
Nov 13 '18 at 18:02
orlambda 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$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