Replace if-statement with a more elegant solution in python [on hold]
up vote
-4
down vote
favorite
I have a code with several if-else statements in python and I want to optimize it without using all those if-elif statements. My code using the input variables with the purpose of output 3 variables by checking 12 specific combinations of the input vars. My code is the following:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
if s_a < 0.3 and r_a == True and g_b == True and (0.33 < s_b < 0.66):
return 1, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == True and (s_b > 0.66):
return 2, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == True and (s_b > 0.66):
return 3, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (0.33 < s_b < 0.66):
return 4, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (s_b > 0.66):
return 5, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == False and (s_b > 0.66):
return 6, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (0.33 < s_b < 0.66):
return 7, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (s_b > 0.66):
return 8, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == True and (s_b > 0.66):
return 9, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (0.33 < s_b < 0.66):
return 10, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (s_b > 0.66):
return 11, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == False and (s_b > 0.66):
return 12, (user_a + user_b), value
else:
return 0, (user_a + user_b), value
Is there a way to write the same code with a more elegant way and avoiding all those if-else statements?
python
New contributor
put on hold as off-topic by Mathias Ettinger, Heslacher, Graipher, πάντα ῥεῖ, Malachi♦ 14 hours ago
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Malachi
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Graipher, πάντα ῥεῖ
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-4
down vote
favorite
I have a code with several if-else statements in python and I want to optimize it without using all those if-elif statements. My code using the input variables with the purpose of output 3 variables by checking 12 specific combinations of the input vars. My code is the following:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
if s_a < 0.3 and r_a == True and g_b == True and (0.33 < s_b < 0.66):
return 1, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == True and (s_b > 0.66):
return 2, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == True and (s_b > 0.66):
return 3, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (0.33 < s_b < 0.66):
return 4, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (s_b > 0.66):
return 5, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == False and (s_b > 0.66):
return 6, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (0.33 < s_b < 0.66):
return 7, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (s_b > 0.66):
return 8, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == True and (s_b > 0.66):
return 9, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (0.33 < s_b < 0.66):
return 10, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (s_b > 0.66):
return 11, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == False and (s_b > 0.66):
return 12, (user_a + user_b), value
else:
return 0, (user_a + user_b), value
Is there a way to write the same code with a more elegant way and avoiding all those if-else statements?
python
New contributor
put on hold as off-topic by Mathias Ettinger, Heslacher, Graipher, πάντα ῥεῖ, Malachi♦ 14 hours ago
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Malachi
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Graipher, πάντα ῥεῖ
If this question can be reworded to fit the rules in the help center, please edit the question.
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
1
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
2
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I have a code with several if-else statements in python and I want to optimize it without using all those if-elif statements. My code using the input variables with the purpose of output 3 variables by checking 12 specific combinations of the input vars. My code is the following:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
if s_a < 0.3 and r_a == True and g_b == True and (0.33 < s_b < 0.66):
return 1, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == True and (s_b > 0.66):
return 2, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == True and (s_b > 0.66):
return 3, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (0.33 < s_b < 0.66):
return 4, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (s_b > 0.66):
return 5, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == False and (s_b > 0.66):
return 6, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (0.33 < s_b < 0.66):
return 7, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (s_b > 0.66):
return 8, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == True and (s_b > 0.66):
return 9, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (0.33 < s_b < 0.66):
return 10, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (s_b > 0.66):
return 11, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == False and (s_b > 0.66):
return 12, (user_a + user_b), value
else:
return 0, (user_a + user_b), value
Is there a way to write the same code with a more elegant way and avoiding all those if-else statements?
python
New contributor
I have a code with several if-else statements in python and I want to optimize it without using all those if-elif statements. My code using the input variables with the purpose of output 3 variables by checking 12 specific combinations of the input vars. My code is the following:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
if s_a < 0.3 and r_a == True and g_b == True and (0.33 < s_b < 0.66):
return 1, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == True and (s_b > 0.66):
return 2, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == True and (s_b > 0.66):
return 3, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (0.33 < s_b < 0.66):
return 4, (user_a + user_b), value
elif s_a < 0.3 and r_a == True and g_b == False and (s_b > 0.66):
return 5, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == True and g_b == False and (s_b > 0.66):
return 6, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (0.33 < s_b < 0.66):
return 7, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == True and (s_b > 0.66):
return 8, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == True and (s_b > 0.66):
return 9, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (0.33 < s_b < 0.66):
return 10, (user_a + user_b), value
elif s_a < 0.3 and r_a == False and g_b == False and (s_b > 0.66):
return 11, (user_a + user_b), value
elif (0.33 < s_a < 0.66) and r_a == False and g_b == False and (s_b > 0.66):
return 12, (user_a + user_b), value
else:
return 0, (user_a + user_b), value
Is there a way to write the same code with a more elegant way and avoiding all those if-else statements?
python
python
New contributor
New contributor
edited 11 hours ago
New contributor
asked 15 hours ago
Jose Ramon
932
932
New contributor
New contributor
put on hold as off-topic by Mathias Ettinger, Heslacher, Graipher, πάντα ῥεῖ, Malachi♦ 14 hours ago
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Malachi
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Graipher, πάντα ῥεῖ
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Mathias Ettinger, Heslacher, Graipher, πάντα ῥεῖ, Malachi♦ 14 hours ago
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Malachi
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Graipher, πάντα ῥεῖ
If this question can be reworded to fit the rules in the help center, please edit the question.
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
1
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
2
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago
add a comment |
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
1
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
2
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
1
1
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
2
2
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Making some educated guesses about typos in your code, you may be able to use something like this:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
ranges = (
(None, 0.3, True, True, 0.33, 0.66),
(None, 0.3, True, True, 0.66, None),
# ...
)
for i, (sa_min, sa_max, ra_val, gb_val, sb_min, sb_max) in enumerate(ranges):
ok = (
(sa_min is None or sa_min < s_a) and
(sa_max is None or s_a < sa_max) and
r_a == ra_val and g_b == gb_val and
(sb_min is None or sb_min < s_b) and
(sb_max is None or s_b < sb_max)
)
if ok:
break
else:
i = -1
return i+1, user_a+user_b, value
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Making some educated guesses about typos in your code, you may be able to use something like this:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
ranges = (
(None, 0.3, True, True, 0.33, 0.66),
(None, 0.3, True, True, 0.66, None),
# ...
)
for i, (sa_min, sa_max, ra_val, gb_val, sb_min, sb_max) in enumerate(ranges):
ok = (
(sa_min is None or sa_min < s_a) and
(sa_max is None or s_a < sa_max) and
r_a == ra_val and g_b == gb_val and
(sb_min is None or sb_min < s_b) and
(sb_max is None or s_b < sb_max)
)
if ok:
break
else:
i = -1
return i+1, user_a+user_b, value
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
add a comment |
up vote
0
down vote
Making some educated guesses about typos in your code, you may be able to use something like this:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
ranges = (
(None, 0.3, True, True, 0.33, 0.66),
(None, 0.3, True, True, 0.66, None),
# ...
)
for i, (sa_min, sa_max, ra_val, gb_val, sb_min, sb_max) in enumerate(ranges):
ok = (
(sa_min is None or sa_min < s_a) and
(sa_max is None or s_a < sa_max) and
r_a == ra_val and g_b == gb_val and
(sb_min is None or sb_min < s_b) and
(sb_max is None or s_b < sb_max)
)
if ok:
break
else:
i = -1
return i+1, user_a+user_b, value
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Making some educated guesses about typos in your code, you may be able to use something like this:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
ranges = (
(None, 0.3, True, True, 0.33, 0.66),
(None, 0.3, True, True, 0.66, None),
# ...
)
for i, (sa_min, sa_max, ra_val, gb_val, sb_min, sb_max) in enumerate(ranges):
ok = (
(sa_min is None or sa_min < s_a) and
(sa_max is None or s_a < sa_max) and
r_a == ra_val and g_b == gb_val and
(sb_min is None or sb_min < s_b) and
(sb_max is None or s_b < sb_max)
)
if ok:
break
else:
i = -1
return i+1, user_a+user_b, value
Making some educated guesses about typos in your code, you may be able to use something like this:
def probs(s_a, s_b, r_a, g_b, user_a, user_b, value):
ranges = (
(None, 0.3, True, True, 0.33, 0.66),
(None, 0.3, True, True, 0.66, None),
# ...
)
for i, (sa_min, sa_max, ra_val, gb_val, sb_min, sb_max) in enumerate(ranges):
ok = (
(sa_min is None or sa_min < s_a) and
(sa_max is None or s_a < sa_max) and
r_a == ra_val and g_b == gb_val and
(sb_min is None or sb_min < s_b) and
(sb_max is None or s_b < sb_max)
)
if ok:
break
else:
i = -1
return i+1, user_a+user_b, value
answered 14 hours ago
Reinderien
1,724616
1,724616
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
add a comment |
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
1
1
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
You should refuse to answer off-topic questions.
– Heslacher
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
@Heslacher It's on-topic, it's just low-quality.
– Reinderien
14 hours ago
add a comment |
What does this code accomplish, and how is it used? See How to Ask.
– 200_success
15 hours ago
My code using the input variables with the purpose of outputing 3 variables by checking 12 specific combinations of the input vars.
– Jose Ramon
15 hours ago
1
That hardly clarifies anything. What do the inputs and outputs mean?
– 200_success
15 hours ago
r_a, g_b are booleans while the rest variables are float. I think the rest is explained in the if-statements. I want to filter those variables and create 12 different output statements.
– Jose Ramon
15 hours ago
2
That also restates the obvious and doesn't explain anything about why this function exists.
– 200_success
15 hours ago