Python 自动创建并向函数传递多个输入
我使用for循环构造了一个字典:Python 自动创建并向函数传递多个输入,python,Python,我使用for循环构造了一个字典: dict = {} for i in np.arange(0, n): dict[i] = some_data[i] 然后我想将字典中的项传递给函数: function(dict[0], dict[1], dict[2], ... dict[n]) 由于n在不同的数据集之间往往有所不同,我是否有办法将多个输入传递给函数,而无需每次手动将dict[0]输入到dict[n] 我试图构造一个包含[dict[0]、dict[1]、…、dict[n]]的数组
dict = {}
for i in np.arange(0, n):
dict[i] = some_data[i]
然后我想将字典中的项传递给函数:
function(dict[0], dict[1], dict[2], ... dict[n])
由于n
在不同的数据集之间往往有所不同,我是否有办法将多个输入传递给函数,而无需每次手动将dict[0]
输入到dict[n]
我试图构造一个包含
[dict[0]、dict[1]、…、dict[n]]
的数组,但函数不接受数组作为输入。是的,您可以,它被称为扩展:
some_data = [4, 5, 6]
# here's another trick:
d = dict(enumerate(some_data))
print(d)
# some function that takes multiple arguments
def f(*args):
print(*args)
# calling such a function with the values of your dictionary, as requested
f(*d.values())
结果:
{0: 4, 1: 5, 2: 6}
4 5 6
这回答了你的问题吗?对这和格里斯玛的回答完美地回答了我的问题。谢谢!