简体   繁体   中英

How do I pass integer items of a list to a function as string arguments in Python?

I have a Python list consisting of integers:

a = [1, 2, 3]

I want to pass the items of this list as arguments to a function, and they must be strings:

myfunc("1", "2", "3")

How can I do it?

So... we use the * operator to use a sequence as multiple arguments for a function call; and we want to convert each argument to a string. The conversion is most obviously and simply done by just passing the value to the builtin str ; we can then just map that conversion function onto the list. These are all elementary techniques and all we have to do is put them together:

myfunc(*map(str, a))

* keyword can unpacking the argument list.

a = [1, 2, 3]
myfunc(*(str(l) for l in a))

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