Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一长串需要作为整数传递给函数的字符串。我现在正在做的是:
my_function(int(list[0]), int(list[1]), int(list[2]), int(list[3])...)
但我知道我可以通过解压缩列表来进行更短的函数调用:
my_function(*list)
我想知道是否有一种方法可以将int()cast 与 list unpacking结合起来*,如下所示:
int()
*
my_function(*int(list)) #Doesn't work
使用内置方法map,例如
map
my_function(*map(int, list))
或者,尝试列表理解:
my_function(*[int(x) for x in list])
顺便提一句:
请不要list用作局部变量的名称,这将隐藏内置方法list。
list
通常使用为变量名附加下划线,否则会隐藏内置方法/与关键字冲突。
映射是答案:
map(int, my_list)