【问题标题】:Python string interpolation with splat operator?使用 splat 运算符的 Python 字符串插值?
【发布时间】:2018-07-20 14:16:29
【问题描述】:

我有一个大的(约 10 个元素)整数列表,我希望将其插入到字符串中。这似乎是 splat 运算符的理想用例,所以我希望能够做这样的事情:

"""[latex]$\begin{bmatrix}
%d & %d \\ %d & %d\end{bmatrix}
\times
\begin{bmatrix}
%d & %d \\ %d & %d
\end{bmatrix} $[/latex]""" % (*lst)
                              ^^^^ SyntaxError: invalid syntax

什么是实现这一目标的简洁、语法有效的方法?

【问题讨论】:

标签: python string-interpolation splat


【解决方案1】:

str.__mod__ 接受一个元组。

'''...''' % tuple(lst)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2013-11-13
    • 2015-10-28
    • 2020-11-22
    相关资源
    最近更新 更多