📅  最后修改于: 2023-12-03 15:17:11.478000             🧑  作者: Mango
If you're looking for a concise way to format a list of integers in Python, lambda expressions can be helpful. The following lambda expression can be used to format a list of integers with a provided separator:
separator = ", "
formatter = lambda x: separator.join(map(str, x))
Here, the map
function converts each integer to a string, and join
concatenates the strings with the separator. You can adjust the separator to fit your needs.
For example, if you have a list of integers [1, 2, 3]
, you can use the lambda expression to format the list as a string:
>>> numbers = [1, 2, 3]
>>> formatted_numbers = formatter(numbers)
>>> formatted_numbers
'1, 2, 3'
You can also use the lambda expression inline, for example in a print
statement:
>>> print("The numbers are: {}".format(formatter(numbers)))
The numbers are: 1, 2, 3
With this lambda expression, you can easily format lists of integers in a concise and flexible way.