Python多重比较样式? [英] Python multiple comparisons style?

查看:50
本文介绍了Python多重比较样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以以更紧凑的方式执行以下操作:

I am wondering if there is a way to do the following in a more compact style:

if (text == "Text1" or text=="Text2" or text=="Text3" or text=="Text4"):
    do_something()

问题是我在if语句中不仅有4个比较,而且开始看起来相当长,模棱两可和丑陋.有什么想法吗?

The problem is i have more than just 4 comparisons in the if statement and it's starting to look rather long, ambiguous, and ugly. Any ideas?

推荐答案

这是怎么回事:

if text in ( 'Text1', 'Text2', 'Text3', 'Text4' ):
    do_something()

我总是发现它简单而优雅.

I've always found that simple and elegant.

这篇关于Python多重比较样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆