Python: “If” function
This is probably really simple but I can't figure it out.
I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.
At the moment I have
if sum(listname[i])==x:
if sum(listname[i])==x-1:
if sum(listname[i])==x-2:
etc
How can I do it so that it is combined in one "if" function.
I've been thinking on the lines of something like:
if sum(listname[i])==x-5>=x>=x+5:
or
if sum(listname[i])==x or x-1 or x-2 ".. etc":
but neither work.
Can anybody shine some light on this?
python list if-statement range
add a comment |
This is probably really simple but I can't figure it out.
I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.
At the moment I have
if sum(listname[i])==x:
if sum(listname[i])==x-1:
if sum(listname[i])==x-2:
etc
How can I do it so that it is combined in one "if" function.
I've been thinking on the lines of something like:
if sum(listname[i])==x-5>=x>=x+5:
or
if sum(listname[i])==x or x-1 or x-2 ".. etc":
but neither work.
Can anybody shine some light on this?
python list if-statement range
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)
– timgeb
Nov 24 '18 at 23:17
1
Try thisif x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:22
add a comment |
This is probably really simple but I can't figure it out.
I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.
At the moment I have
if sum(listname[i])==x:
if sum(listname[i])==x-1:
if sum(listname[i])==x-2:
etc
How can I do it so that it is combined in one "if" function.
I've been thinking on the lines of something like:
if sum(listname[i])==x-5>=x>=x+5:
or
if sum(listname[i])==x or x-1 or x-2 ".. etc":
but neither work.
Can anybody shine some light on this?
python list if-statement range
This is probably really simple but I can't figure it out.
I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.
At the moment I have
if sum(listname[i])==x:
if sum(listname[i])==x-1:
if sum(listname[i])==x-2:
etc
How can I do it so that it is combined in one "if" function.
I've been thinking on the lines of something like:
if sum(listname[i])==x-5>=x>=x+5:
or
if sum(listname[i])==x or x-1 or x-2 ".. etc":
but neither work.
Can anybody shine some light on this?
python list if-statement range
python list if-statement range
asked Nov 24 '18 at 23:15
P erryP erry
455
455
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)
– timgeb
Nov 24 '18 at 23:17
1
Try thisif x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:22
add a comment |
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)
– timgeb
Nov 24 '18 at 23:17
1
Try thisif x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:22
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.
if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)– timgeb
Nov 24 '18 at 23:17
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.
if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)– timgeb
Nov 24 '18 at 23:17
1
1
Try this
if x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5– Wariored
Nov 24 '18 at 23:22
Try this
if x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5– Wariored
Nov 24 '18 at 23:22
add a comment |
4 Answers
4
active
oldest
votes
A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc":
(which is not valid python) is usually solved with if value in range(start, stop, step):
So you would write:
if sum(listname[i) in range(x-2, x):
# Code for this case here...
add a comment |
Do you simply mean
if x-5 <= sum(listname[i]) <= x+5:
...
...
1
You might want to exchange>=
with<=
. Otherwise good answer.
– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
add a comment |
It looks like you want to check if the sum of the list is between x - 5
and x + 5
. To put it in one if statement is simply:
s = sum(listname[i])
if s >= x - 5 and s <= x + 5:
# do stuff
add a comment |
From what I understand from your question. You what to check that whether sum(listname[i])
is between (x-5, x+5)
You can do this in a single if assuming x is a possitive value:
if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))
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%2f53463211%2fpython-if-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc":
(which is not valid python) is usually solved with if value in range(start, stop, step):
So you would write:
if sum(listname[i) in range(x-2, x):
# Code for this case here...
add a comment |
A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc":
(which is not valid python) is usually solved with if value in range(start, stop, step):
So you would write:
if sum(listname[i) in range(x-2, x):
# Code for this case here...
add a comment |
A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc":
(which is not valid python) is usually solved with if value in range(start, stop, step):
So you would write:
if sum(listname[i) in range(x-2, x):
# Code for this case here...
A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc":
(which is not valid python) is usually solved with if value in range(start, stop, step):
So you would write:
if sum(listname[i) in range(x-2, x):
# Code for this case here...
answered Nov 24 '18 at 23:23
Charles LandauCharles Landau
2,6931216
2,6931216
add a comment |
add a comment |
Do you simply mean
if x-5 <= sum(listname[i]) <= x+5:
...
...
1
You might want to exchange>=
with<=
. Otherwise good answer.
– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
add a comment |
Do you simply mean
if x-5 <= sum(listname[i]) <= x+5:
...
...
1
You might want to exchange>=
with<=
. Otherwise good answer.
– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
add a comment |
Do you simply mean
if x-5 <= sum(listname[i]) <= x+5:
...
...
Do you simply mean
if x-5 <= sum(listname[i]) <= x+5:
...
...
edited Nov 25 '18 at 4:44
Wariored
373312
373312
answered Nov 24 '18 at 23:22
chrischris
211
211
1
You might want to exchange>=
with<=
. Otherwise good answer.
– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
add a comment |
1
You might want to exchange>=
with<=
. Otherwise good answer.
– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
1
1
You might want to exchange
>=
with <=
. Otherwise good answer.– a_guest
Nov 24 '18 at 23:26
You might want to exchange
>=
with <=
. Otherwise good answer.– a_guest
Nov 24 '18 at 23:26
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
x-5 cannot be superior to x+5
– Wariored
Nov 24 '18 at 23:27
add a comment |
It looks like you want to check if the sum of the list is between x - 5
and x + 5
. To put it in one if statement is simply:
s = sum(listname[i])
if s >= x - 5 and s <= x + 5:
# do stuff
add a comment |
It looks like you want to check if the sum of the list is between x - 5
and x + 5
. To put it in one if statement is simply:
s = sum(listname[i])
if s >= x - 5 and s <= x + 5:
# do stuff
add a comment |
It looks like you want to check if the sum of the list is between x - 5
and x + 5
. To put it in one if statement is simply:
s = sum(listname[i])
if s >= x - 5 and s <= x + 5:
# do stuff
It looks like you want to check if the sum of the list is between x - 5
and x + 5
. To put it in one if statement is simply:
s = sum(listname[i])
if s >= x - 5 and s <= x + 5:
# do stuff
answered Nov 24 '18 at 23:25
GniemGniem
914
914
add a comment |
add a comment |
From what I understand from your question. You what to check that whether sum(listname[i])
is between (x-5, x+5)
You can do this in a single if assuming x is a possitive value:
if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))
add a comment |
From what I understand from your question. You what to check that whether sum(listname[i])
is between (x-5, x+5)
You can do this in a single if assuming x is a possitive value:
if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))
add a comment |
From what I understand from your question. You what to check that whether sum(listname[i])
is between (x-5, x+5)
You can do this in a single if assuming x is a possitive value:
if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))
From what I understand from your question. You what to check that whether sum(listname[i])
is between (x-5, x+5)
You can do this in a single if assuming x is a possitive value:
if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))
answered Nov 24 '18 at 23:31
Talip Tolga SarıTalip Tolga Sarı
936
936
add a comment |
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%2f53463211%2fpython-if-function%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
This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky.
if
is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)– timgeb
Nov 24 '18 at 23:17
1
Try this
if x-5 <= sum(listname[i]) <= x+5:
. If x is a real number x-5 cannot be superior to x+5– Wariored
Nov 24 '18 at 23:22