python - 如何将可变数量的参数格式化为字符串? [英] python - How to format variable number of arguments into a string?

查看:29
本文介绍了python - 如何将可变数量的参数格式化为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道可以在字符串中使用 one %s 来格式化 one 参数:

<预><代码>>>>"你好 %s" % "世界"'你好,世界'

对于两个参数,我们可以使用两个 %s(废话!):

<预><代码>>>>"你好 %s, %s" % ("John", "Joe")'你好约翰,乔'

那么,如何格式化可变数量的参数而不必在基本字符串中显式定义数量等于要格式化的参数数量的 %s?如果存在这样的东西,那就太酷了:

<预><代码>>>>你好<cool_operator_here>"%(约翰"、乔"、玛丽")你好约翰乔玛丽>>>你好<cool_operator_here>"%(约翰"、乔"、玛丽"、瑞克"、苏菲")你好约翰乔玛丽瑞克苏菲

这是否可能,或者我唯一能做的就是做这样的事情:

<预><代码>>>>my_args = [约翰"、乔"、玛丽"]>>>my_str = "你好" + ("".join(["%s"] * len(my_args)))>>>my_str % 元组(my_args)你好约翰乔玛丽"

注意:我需要使用 %s 字符串格式化操作符来完成.

更新:

它需要与 %s 一起使用,因为另一个库中的函数使用该运算符格式化我的字符串,前提是我传递了未格式化的字符串和 args 以对其进行格式化,但它进行了一些检查和在实际进行格式化之前对 args 进行更正(如果需要).

所以我需要调用它:

<预><代码>>>>function_in_library("Hello ", ["John", "Joe", "Mary"])你好约翰乔玛丽"

感谢您的帮助!

解决方案

您将在列表中使用 str.join() 没有 字符串格式,然后插入结果:

"你好 %s" % ', '.join(my_args)

演示:

<预><代码>>>>my_args = ["foo", "bar", "baz"]>>>你好 %s" % ', '.join(my_args)'你好 foo, bar, baz'

如果您的某些参数还不是字符串,请使用列表推导式:

<预><代码>>>>my_args = ["foo", "bar", 42]>>>"你好 %s" % ', '.join([str(e) for e in my_args])'你好 foo,酒吧,42'

或使用 map(str, ...):

<预><代码>>>>你好 %s" % ', '.join(map(str, my_args))'你好 foo,酒吧,42'

你会对你的函数做同样的事情:

function_in_library("你好 %s", ', '.join(my_args))

如果您受到不能在插值参数列表中使用 join 的(相当任意的)限制,请改用 join 来创建格式化字符串:

function_in_library("Hello %s" % ', '.join(['%s'] * len(my_args)), my_args)

We know that formatting one argument can be done using one %s in a string:

>>> "Hello %s" % "world"
'Hello world'

for two arguments, we can use two %s (duh!):

>>> "Hello %s, %s" % ("John", "Joe")
'Hello John, Joe'

So, how can I format a variable number of arguments without having to explicitly define within the base string a number of %s equal to the number of arguments to format? it would be very cool if something like this exists:

>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary")
Hello JohnJoeMary
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary", "Rick", "Sophie")
Hello JohnJoeMaryRickSophie

Is this even possible or the only thing I could do about it is to do something like:

>>> my_args = ["John", "Joe", "Mary"]
>>> my_str = "Hello " + ("".join(["%s"] * len(my_args)))
>>> my_str % tuple(my_args)
"Hello JohnJoeMary"

NOTE: I need to do it with the %s string formatting operator.

UPDATE:

It needs to be with the %s because a function from another library formats my string using that operator given that I pass the unformatted string and the args to format it, but it makes some checking and corrections (if needed) on the args before actually making the formatting.

So I need to call it:

>>> function_in_library("Hello <cool_operator_here>", ["John", "Joe", "Mary"])
"Hello JohnJoeMary"

Thanks for your help!

解决方案

You'd use str.join() on the list without string formatting, then interpolate the result:

"Hello %s" % ', '.join(my_args)

Demo:

>>> my_args = ["foo", "bar", "baz"]
>>> "Hello %s" % ', '.join(my_args)
'Hello foo, bar, baz'

If some of your arguments are not yet strings, use a list comprehension:

>>> my_args = ["foo", "bar", 42]
>>> "Hello %s" % ', '.join([str(e) for e in my_args])
'Hello foo, bar, 42'

or use map(str, ...):

>>> "Hello %s" % ', '.join(map(str, my_args))
'Hello foo, bar, 42'

You'd do the same with your function:

function_in_library("Hello %s", ', '.join(my_args))

If you are limited by a (rather arbitrary) restriction that you cannot use a join in the interpolation argument list, use a join to create the formatting string instead:

function_in_library("Hello %s" % ', '.join(['%s'] * len(my_args)), my_args)

这篇关于python - 如何将可变数量的参数格式化为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆