实际上,我同意AndrewHare的回答,只需传递一个包含单个元素的列表即可。
我会这样做:
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)
并期望该参数将始终是一个列表-即使它只是一个元素的列表。
记得:
要求宽恕比允许容易。
但是,如果您真的必须接受一个非列表,那么在这种情况下将其变成一个列表呢?
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”列表可能很长,您可能要考虑做的另一件事:考虑强制转换将其设置为一组,以便查找更高效。
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)



