using a list as values to a format string without having to list each item separately
def createSVGStyleTransformationMatrix(self, transformMatrix):
return "matrix(%f %f %f %f %f %f)"%(transformMatrix[0],transformMatrix[1],transformMatrix[2],transformMatrix[3],transformMatrix[4],transformMatrix[5])
As all the substitutions are from the same list and in sequence I was hoping there was a neater way to 开发者_开发知识库do this. Anybody got any ideas?
Alternatively, use the str.format
method (version 2.6+), which is preferred over string interpolation(%
) in Python 3.x:
return "matrix(%f %f %f %f %f %f)".format(*transformMatrix)
The *
instructs the Python interpreter to unpack the arguments from a list or tuple.
If len(transformMatrix) == 6
, then you can use
return "matrix(%f %f %f %f %f %f)" % tuple(transformMatrix)
"matrix(%s)" % " ".join(["%f" % i for i in transformMatrix])
(not tested)
EDIT: tested :-)
"matrix({})".format(" ".join(map(str, transformMatrix)))
or I think it's simplier:
"matrix(" + " ".join(map(str, transformMatrix) + ")"
String concatenation shouldn't be a problem with three small strings, but should avoid it using large number of or big strings.
精彩评论