<tfoot id='TJYhl'></tfoot>

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

    • <bdo id='TJYhl'></bdo><ul id='TJYhl'></ul>

      1. <small id='TJYhl'></small><noframes id='TJYhl'>

        将列表或单个整数作为参数处理

        时间:2023-09-11

        <tfoot id='RSsPs'></tfoot>
        • <legend id='RSsPs'><style id='RSsPs'><dir id='RSsPs'><q id='RSsPs'></q></dir></style></legend>

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

                <tbody id='RSsPs'></tbody>
                <bdo id='RSsPs'></bdo><ul id='RSsPs'></ul>

                • <small id='RSsPs'></small><noframes id='RSsPs'>

                  本文介绍了将列表或单个整数作为参数处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  函数应根据行名(在本例中为第 2 列)选择表中的行.它应该能够将单个名称或名称列表作为参数并正确处理它们.

                  A function should select rows in a table based on the row name (column 2 in this case). It should be able to take either a single name or a list of names as arguments and handle them correctly.

                  这就是我现在所拥有的,但理想情况下不会有这种重复的代码,并且会智能地使用异常之类的东西来选择正确的方式来处理输入参数:

                  This is what I have now, but ideally there wouldn't be this duplicated code and something like exceptions would be used intelligently to choose the right way to handle the input argument:

                  def select_rows(to_select):
                      # For a list
                      for row in range(0, table.numRows()):
                          if _table.item(row, 1).text() in to_select:
                              table.selectRow(row)
                      # For a single integer
                      for row in range(0, table.numRows()):
                          if _table.item(row, 1).text() == to_select:
                              table.selectRow(row)
                  

                  推荐答案

                  其实我同意Andrew Hare的回答,只是传递一个包含单个元素的列表.

                  Actually I agree with Andrew Hare's answer, just pass a list with a single element.

                  但如果你真的必须接受一个非列表,那么在这种情况下将它变成一个列表怎么样?

                  But if you really must accept a non-list, how about just turning it into a list in that case?

                  def select_rows(to_select):
                      if type(to_select) is not list: to_select = [ to_select ]
                  
                      for row in range(0, table.numRows()):
                          if _table.item(row, 1).text() in to_select:
                              table.selectRow(row)
                  

                  在单个项目列表上执行in"的性能损失不太可能很高 :-)但这确实指出了如果您的to_select"列表可能很长,您可能需要考虑做的另一件事:考虑将其转换为一个集合,以便查找更有效.

                  The performance penalty for doing 'in' on a single-item list isn't likely to be high :-) But that does point out one other thing you might want to consider doing if your 'to_select' list may be long: consider casting it to a set so that lookups are more efficient.

                  def select_rows(to_select):
                      if type(to_select) is list: to_select = set( to_select )
                      elif type(to_select) is not set: to_select = set( [to_select] )
                  
                      for row in range(0, table.numRows()):
                          if _table.item(row, 1).text() in to_select:
                              table.selectRow(row)
                  

                  这篇关于将列表或单个整数作为参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 int 转换为 ASCII 并返回 Python 下一篇:Python:为什么 `random.randint(a, b)` 返回一个包含 `b

                  相关文章

                  最新文章

                  <legend id='P6dGq'><style id='P6dGq'><dir id='P6dGq'><q id='P6dGq'></q></dir></style></legend>

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

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