[英]what is best way to indentify that the length of tuple in a list is 0 using python
[英]What is the best way to call API for each data in the list/tuple in python?
我有一个t =(10,20,30)的元组。 我想为该元组t的每个成员调用一个API。 显然,我可以循环并调用API-元组中的每个项目的f(“ hello”,b,10),f(“ hello”,b,20),f(“ hello”,b,20)。
但是,我正在寻找在Python中执行此操作的最佳方法。
我想在这里使用地图或lambda。 我该怎么办?
如何使用一些默认参数进行映射?
显式循环的一种替代方法是map
:
tuple(map(f, t))
# or
list(map(f, t))
这也提供了将线程或进程池用于较大任务的可能性。
以for循环为例
for a in t:
f("hello", b, a)
列表理解为您提供了一个结果列表,该列表可能不是您期望的:
[ f("hello", b, a) for a in t ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.