I have a piece of code that looks like this:
myfunction(a, b, c)
myfunction(d, e, f)
myfunction(g, h, i)
myfunction(j, k, l)
The number of arguments do not change, but the function has to be called consecutively with different values each time. These values are not automatically generated and are manually inputted. Is there an inline solution to do this without creating a function to call this function? Something like:
myfunction(a, b, c)(d, e f)(g, h, i)(j, k, l)
Any help appreciated. Thanks in advance!
Simple, use tuple unpacking
tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]
for tripple in tripples:
print(myfunction(*tripple))
I'm surprised that nobody mentioned map
map(func, iter)
It maps each iterable in iter
to the func
passed in the first argument.
For your use case it should look like
map(myfunction, *zip((a, b, c), (d, e, f), (g, h, i), (j, k, l)))
Hi I am not sure whether it's the most pythonic way , but if you have arguments defined as in a list the you can call the function in a loop, and remove the n argument from the beginning of the list :
Have a look to the sample code :
def myfunction(x1,x2,x3):
return x1+x2+x3
arglist = [1,2,3,4,5,6,7,8,9]
for _ in range(3):
print myfunction(arglist[_+0],arglist[_+1],arglist[_+2])
# remove used arguments from the list
arglist= arglist[2:]
你可以在这里滥用列表理解
[myfunction(*item) for item in ((a, b, c), (d, e, f), (g, h, i), (j, k, l))]
我想你想做的是这样的......
myfunction(myfunction(d, e, f), myfunction(g, h, i), myfunction(j, k, l))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.