Python 将列表或单个整数作为参数处理
函数应根据行名称(本例中为第2列)选择表中的行。它应该能够将单个名称或名称列表作为参数,并正确处理它们 这就是我现在所拥有的,但理想情况下不会有这种重复的代码,并且会智能地使用类似异常的东西来选择正确的方法来处理输入参数:Python 将列表或单个整数作为参数处理,python,list,function,integer,Python,List,Function,Integer,函数应根据行名称(本例中为第2列)选择表中的行。它应该能够将单个名称或名称列表作为参数,并正确处理它们 这就是我现在所拥有的,但理想情况下不会有这种重复的代码,并且会智能地使用类似异常的东西来选择正确的方法来处理输入参数: def select_rows(to_select): # For a list for row in range(0, table.numRows()): if _table.item(row, 1).text() in to_select:
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)
我会这样做:
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(*arguments):
for row in range(0, table.numRows()):
if _table.item(row, 1).text() in arguments:
table.selectRow(row)
select_rows('abc')
select_rows('abc', 'def')
然后您可以像这样传递一个参数:
def select_rows(*arguments):
for row in range(0, table.numRows()):
if _table.item(row, 1).text() in arguments:
table.selectRow(row)
select_rows('abc')
select_rows('abc', 'def')
多个参数如下所示:
def select_rows(*arguments):
for row in range(0, table.numRows()):
if _table.item(row, 1).text() in arguments:
table.selectRow(row)
select_rows('abc')
select_rows('abc', 'def')
如果您已经有一个列表:
items = ['abc', 'def']
select_rows(*items)
实际上我同意,只需传递一个包含单个元素的列表
但是如果你真的必须接受一个非列表,那么在这种情况下把它变成一个列表怎么样
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”操作的性能损失可能不会很高:-)
但是,如果你的“ToSeLoad”列表可能很长,那么你可能需要考虑另一件事:考虑把它投射到一个集合中,这样查找更有效。
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)
我同意Sharkey的版本,但使用更多的duck类型:
def select_rows(to_select):
try:
len(to_select)
except TypeError:
to_select = [to_select]
for row in range(0, table.numRows()):
if _table.item(row, 1).text() in to_select:
table.selectRow(row)
这有利于处理支持in运算符的任何对象。此外,如果给上一个版本一个元组或其他序列,它只会将其包装在一个列表中。缺点是使用异常处理会带来一些性能损失。一个简单的包装器可以处理列表或单个对象的情况
def wrap_list(val):
if type(val) is list:
return val
return [val]
使用numpy,有点不一般但简洁:
numpy.unique()
通过仅保留数组(如或标量)的唯一值来自动处理,并返回唯一值或标量的列表
import numpy as np
def select_rows(to_select):
to_select = np.unique(to_select)
for row in range(0, table.numRows()):
if _table.item(row, 1).text() in to_select:
table.selectRow(row)
+1... 只维护一组代码来执行任务要容易得多,而且更具Python风格;如果有人无视文件的要求,就让它爆炸吧。如果确实需要一个接受单个整数作为参数的函数,则制作第二个名为“def select_row(to_select)”,并将其打包为“to_select”列表,然后调用select_rows。+1类似这种方法比Andrew Hare的更好。。。问题可能是您需要向同一个函数传递更多参数,而不仅仅是列表/单个参数。但您可以在之前使用这些参数,也可以使用关键字参数,即。**kwargs。这个答案显然更好+1自我记录代码*args请求一个iterable。这一个对于Unicode和字符串是有问题的。cf:有效点,至少应该是“in(list,tuple)”。。。或者“不在(字符串,unicode)”中。我想,您最好直接查找“ThisThingy是否支持'in'”。事实上,Python不像其他语言那样对异常处理有性能损失。例如,请参见:目前认为使用
isinstance
比使用type
更好,请参见,如果使用isinstance
,则某些类型存在2/3兼容性问题。例如,字符串类型是str(在3中)或basestring(在2.7中)。您可以为此使用six.string\u类型
或six.integer\u类型
。不幸的是,没有six.float\u类型
,因此如果需要Python2.7中的long
,则必须手动编写。