![](/img/trans.png)
[英]Perfplot to compare performance of several functions with multiple arguments (more than one)
[英]How to use python Maps on functions with more than one arguments
NAME_LIST = ["abc1","abc2","cde1"]
class abc:
@staticmethod
def foo(a,name,*args):
a = do_something(name,*args)
return do_another(a)
當有多個函數參數時,如何使用 python 映射傳入名稱列表並打印輸出
像這樣的東西,我想保留 a,*args 作為常量(但不想讓它成為默認參數)
list(map(abc.foo, NAME_LIST))
我想將NAME_LIST
變量傳遞給name
你可以做什么:
def myFunc(x,y, *args):
return x*y+sum(args)
x=[[1,4,5,5,6,7], [3,2,1], [7,8], [2,9,0,4,3]]
y=list(map(lambda a: myFunc(*a), x))
y
#outputs:
[27, 7, 56, 25]
所以在你的情況下:
NAME_LIST = ["abc1","abc2","cde1"]
class abc:
@staticmethod
def foo(a,name,*args):
a = do_something(name,*args)
return do_another(a)
list(map(lambda x: abc.foo(*x), [NAME_LIST]))
只是一個筆記map
迭代提供的可迭代對象,因此它將使用傳遞列表的每個單獨元素執行您的函數,如果您將其保留為NAME_LIST
每次將是單個字符串 - 因此您的函數將失敗,因為它有 2 個位置參數,所以這是您要map
的列表的每個元素的最小大小 - 因此[NAME_LIST]
。
希望這是有道理的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.