简体   繁体   中英

What is the best way to call API for each data in the list/tuple in python?

I have a tuple with t = (10,20,30). I want to call an API for each of the member of this tuple t. Obviously, I can loop and call API - f("hello", b,10), f("hello",b,20), f("hello",b,20) for each item in the tuple.

However, I am looking for a best way to do this in Python.

I want to use map here or lambda. How can I do so?

How do I map with some default parameters?

One alternative to an explicit loop is map :

tuple(map(f, t))

# or

list(map(f, t))

This also opens the possibility to use thread or process pools for larger tasks.

With a for loop it's for example

for a in t:
    f("hello", b, a)

The list comprehension gives you a list as a result, which may not be what you expect:

[ f("hello", b, a) for a in t ]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM