缩短Python中的许多if子句(Shorting down many if clauses in Python)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
缩短Python中的许多if子句(Shorting down many if clauses in Python)

我有这个代码,我想把它扩展到一个非常高的数量,但它在我做它的方式是非常低效...也许这里有一个人知道一个简单的方法来缩短它。

if draw_x+draw_x1>50: space_x=10 if draw_x+draw_x1>500: space_x=100 if draw_x+draw_x1>5000: space_x=1000 if draw_x+draw_x1>50000: space_x=10000 if draw_x+draw_x1>500000: space_x=100000 if draw_x+draw_x1>5000000: space_x=1000000

I've got this code and i want to extend it to a veryhigh amount but it is veryy inefficient in the way I'm doing it... Maybe some one here knows a easy way to short it down.

if draw_x+draw_x1>50: space_x=10 if draw_x+draw_x1>500: space_x=100 if draw_x+draw_x1>5000: space_x=1000 if draw_x+draw_x1>50000: space_x=10000 if draw_x+draw_x1>500000: space_x=100000 if draw_x+draw_x1>5000000: space_x=1000000

最满意答案

怎么样:

space_x = 10**int(math.log10((draw_x+draw_x1)//5))

How about:

space_x = 10**int(math.log10((draw_x+draw_x1)//5))

更多推荐

本文发布于:2023-04-12 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/dbeb2932d9743b9ce6b5e2a163e79524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   Python   clauses   Shorting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!