Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to determine the level of a number without using If-Elif statement in Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to judge the grade of a number without using If-Elif sentence in Python". In daily operation, it is believed that many people have doubts about how to judge the grade of a number without using If-Elif sentence in Python. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful to answer the question of "how to judge the level of a number without using If-Elif statement in Python"! Next, please follow the editor to study!

The main idea of the topic is: there are five grades from A to F, and it is necessary to determine the level to which a certain value (from 0 to 1) belongs. For example, if the value > = 0.9, it belongs to A; if the value > = 0.8, it belongs to B; and so on.

If you use the if-elif statement, you might write something like this:

If scr > = 0.9: print ('A') elif scr > = 0.8: print ('B') elif scr > = 0.7: print ('C') elif scr > = 0.6: print ('D') else: print ('F')

There are a lot of repetitive patterns in this writing, which are not concise and elegant. What better way to write to achieve this goal?

There are many answers to this question, and there are a variety of ways to realize it. I'll pick a few that are more readable:

Method 1: use bisect module (digitally adjustable)

Method 2: use zip () and next ()

Method 3: use dictionaries (only for ordered dictionaries above Python 3.6)

There are several other answers, all of which can achieve the purpose of digital grading, but they are much less readable because they either require you to calculate and reason, or introduce additional variables.

If you are interested, you can check all the answers at this address: https://stackoverflow.com/questions/61030617/how-can-i-simplify-repetitive-if-elif-statements

After looking at all the answers, I think the method of using bisect is more efficient and elegant, and it deserves a high approval vote.

Here is a brief analysis of its implementation process.

Bisect is the built-in standard library of Python and implements the binary search algorithm. The so-called binary search, also known as "Binary Search", its basic idea is to divide the ordered n elements into two halves, and then compare the x to be found with the intermediate element. if x is less than the intermediate element, the left half is bisected, and then x is compared with the intermediate element, and so on.

This is a simple graphical example:

The bisect () method in the bisect library finds the insertion point I of element x in an ascending sequence, so that the elements on the left side of the insertion point are less than or equal to x, and the elements on the right side of the insertion point are greater than x.

Compared with the previous example:

From bisect import bisect def grade (score, breakpoints= [60, 70, 80, 90], grades='FDCBA'): I = bisect (breakpoints, score) return grades [I]

Can be simplified into two parts:

Bisect ([60, 70, 80, 90], score), which returns the value of the insertion point. If score is 59, it is calculated that the insertion point is on the left side of 60, and the index value of the Python list starts with 0, so the return insertion point value is 0; if score is 60, the insertion point is calculated to the right of 60, that is, the return index value is 1.

'FDCBA' [I] returns a character with an index value of I. If I is 0, get "F"; if I is 3, get "B".

Binary search algorithm is an efficient algorithm with a time complexity of O (logn). The search scope of this topic is very small, so there is little difference in time efficiency. But its writing method can be called Pythonic, which is worth using for reference.

In addition, take a look at the previous method 3 (using a dictionary), which is very readable, that is, the scr is compared sequentially with the values in the dictionary (from high to low, that is, 0.9-0.5) to get the corresponding key value. (PS: it has an extra "E" grade, which can be removed.)

If the Python version is less than 3. 6, grades.items () will be unordered and will break the order of comparisons. For compatibility, you can change it to sorted (grades.items ()):

This method of writing does not introduce additional libraries, and the items () and sorted () used are both basic knowledge (compared to the zip () and next () of method 2), simple and practical, and highly recommended.

In any case, the way to judge the repeated use of if-elif statements is clumsy and must be improved.

At this point, the study on "how to determine the level of a number without using If-Elif statements in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report