擅长:python、mysql、java
<p>根据这个<a href="https://sites.google.com/site/kittipat/programming-with-python/formatanddisplayelementsinpythonlist">resource page</a>,如果x的长度是变化的,我们可以使用:</p>
<pre><code>', '.join(['%.2f']*len(x))
</code></pre>
<p>为列表中的每个元素创建一个占位符。下面是一个例子:</p>
<pre><code>x = [1/3.0, 1/6.0, 0.678]
s = ("elements in the list are ["+', '.join(['%.2f']*len(x))+"]") % tuple(x)
print s
>>> elements in the list are [0.33, 0.17, 0.68]
</code></pre>