<small id='SWme6'></small><noframes id='SWme6'>

    <tfoot id='SWme6'></tfoot>

      1. <legend id='SWme6'><style id='SWme6'><dir id='SWme6'><q id='SWme6'></q></dir></style></legend>
        <i id='SWme6'><tr id='SWme6'><dt id='SWme6'><q id='SWme6'><span id='SWme6'><b id='SWme6'><form id='SWme6'><ins id='SWme6'></ins><ul id='SWme6'></ul><sub id='SWme6'></sub></form><legend id='SWme6'></legend><bdo id='SWme6'><pre id='SWme6'><center id='SWme6'></center></pre></bdo></b><th id='SWme6'></th></span></q></dt></tr></i><div id='SWme6'><tfoot id='SWme6'></tfoot><dl id='SWme6'><fieldset id='SWme6'></fieldset></dl></div>
        • <bdo id='SWme6'></bdo><ul id='SWme6'></ul>

        对 IF 语句使用 OR 比较

        时间:2023-09-01

            <tbody id='fuqFO'></tbody>
        1. <tfoot id='fuqFO'></tfoot>

            <i id='fuqFO'><tr id='fuqFO'><dt id='fuqFO'><q id='fuqFO'><span id='fuqFO'><b id='fuqFO'><form id='fuqFO'><ins id='fuqFO'></ins><ul id='fuqFO'></ul><sub id='fuqFO'></sub></form><legend id='fuqFO'></legend><bdo id='fuqFO'><pre id='fuqFO'><center id='fuqFO'></center></pre></bdo></b><th id='fuqFO'></th></span></q></dt></tr></i><div id='fuqFO'><tfoot id='fuqFO'></tfoot><dl id='fuqFO'><fieldset id='fuqFO'></fieldset></dl></div>

            <small id='fuqFO'></small><noframes id='fuqFO'>

          1. <legend id='fuqFO'><style id='fuqFO'><dir id='fuqFO'><q id='fuqFO'></q></dir></style></legend>
              <bdo id='fuqFO'></bdo><ul id='fuqFO'></ul>

                  本文介绍了对 IF 语句使用 OR 比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中使用 IF 语句时,您必须执行以下操作才能使级联"正常工作.

                  When using IF statements in Python, you have to do the following to make the "cascade" work correctly.

                  if job == "mechanic" or job == "tech":
                          print "awesome"
                  elif job == "tool" or job == "rock":
                          print "dolt"
                  

                  有没有办法让 Python 在检查等于"时接受多个值?例如,

                  Is there a way to make Python accept multiple values when checking for "equals to"? For example,

                  if job == "mechanic" or "tech":
                      print "awesome"
                  elif job == "tool" or "rock":
                      print "dolt"
                  

                  推荐答案

                  if job in ("mechanic", "tech"):
                      print "awesome"
                  elif job in ("tool", "rock"):
                      print "dolt"
                  

                  括号中的值是一个元组.in 运算符检查左侧项目是否出现在右侧句柄元组内的某个位置.

                  The values in parentheses are a tuple. The in operator checks to see whether the left hand side item occurs somewhere inside the right handle tuple.

                  请注意,当 Python 使用 in 运算符搜索元组或列表时,它会进行线性搜索.如果右侧有大量项目,这可能是性能瓶颈.一种更大规模的方法是使用 frozenset:

                  Note that when Python searches a tuple or list using the in operator, it does a linear search. If you have a large number of items on the right hand side, this could be a performance bottleneck. A larger-scale way of doing this would be to use a frozenset:

                  AwesomeJobs = frozenset(["mechanic", "tech", ... lots of others ])
                  def func():
                      if job in AwesomeJobs:
                          print "awesome"
                  

                  如果在程序运行期间不需要更改出色作业列表,则首选使用 frozenset 而不是 set.

                  The use of frozenset over set is preferred if the list of awesome jobs does not need to be changed during the operation of your program.

                  这篇关于对 IF 语句使用 OR 比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 中的“x = y 或 z"赋值有什么作用? 下一篇:Python Bool 和 int 比较以及使用布尔值对列表进行索引

                  相关文章

                      <bdo id='RT6FQ'></bdo><ul id='RT6FQ'></ul>
                    <legend id='RT6FQ'><style id='RT6FQ'><dir id='RT6FQ'><q id='RT6FQ'></q></dir></style></legend>
                      <i id='RT6FQ'><tr id='RT6FQ'><dt id='RT6FQ'><q id='RT6FQ'><span id='RT6FQ'><b id='RT6FQ'><form id='RT6FQ'><ins id='RT6FQ'></ins><ul id='RT6FQ'></ul><sub id='RT6FQ'></sub></form><legend id='RT6FQ'></legend><bdo id='RT6FQ'><pre id='RT6FQ'><center id='RT6FQ'></center></pre></bdo></b><th id='RT6FQ'></th></span></q></dt></tr></i><div id='RT6FQ'><tfoot id='RT6FQ'></tfoot><dl id='RT6FQ'><fieldset id='RT6FQ'></fieldset></dl></div>
                    1. <tfoot id='RT6FQ'></tfoot>

                      <small id='RT6FQ'></small><noframes id='RT6FQ'>