例如:
>>> Spoken = namedtuple("Spoken", ["loudness", "pitch"])
>>> s = Spoken(loudness=90, pitch='high')
>>> str(s)
"Spoken(loudness=90, pitch='high')"
Run Code Online (Sandbox Code Playgroud)
我想要的是:
>>> str(s)
90
Run Code Online (Sandbox Code Playgroud)
那就是我希望字符串表示显示响度属性.这可能吗 ?
正如标题所述,这两面旗帜有什么区别?看来他们都使用repr()将值转换为字符串?此外,在这行代码中:
"{0!r:20}".format("Hello")
Run Code Online (Sandbox Code Playgroud)
!r前面的0是什么?
我想比较不同以在Python中用不同的变量构建一个字符串:
+来连接(被称为"加号")%"".join(list)format功能"{0.<attribute>}".format(object)我比较了3种类型的情景
我每次测量100万次操作并且平均执行超过6次测量.我想出了以下时间:
在每个场景中,我得出以下结论
%比使用format函数格式化要快得多我相信format比%(例如在这个问题中)要好得多,并且%几乎被弃用了.
因此,我有几个问题:
%真的比快format?"{} {}".format(var1, var2)效率更高"{0.attribute1} {0.attribute2}".format(object)?作为参考,我使用以下代码来测量不同的时序.
import time
def timing(f, n, show, *args):
if show: print f.__name__ + ":\t",
r = range(n/10)
t1 = time.clock()
for i in r:
f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); …Run Code Online (Sandbox Code Playgroud) 我的代码
$ python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = (1, 2)
>>> '%d %d %d' % (0, *a)
'0 1 2'
>>> '%d %d %d' % (*a, 3)
'1 2 3'
>>> '%d %d' % (*a)
File "<stdin>", line 1
SyntaxError: can't use starred expression here
>>>
Run Code Online (Sandbox Code Playgroud)
我的问题,为什么?
用更严肃的语气:我想要一个答案或参考,详细说明使用星号表达的所有细节,因为碰巧我有时会对其行为感到惊讶......
为了反映我的问题后面的一些启发性评论,我添加了以下代码
>>> '%d %d' % (, …Run Code Online (Sandbox Code Playgroud) 以前您将使用gettext以下内容:
_('Hey {},').format(username)
Run Code Online (Sandbox Code Playgroud)
但是新的Python的f-string怎么样?
f'Hey {username}'
Run Code Online (Sandbox Code Playgroud) 在这篇关于SQLite的帖子中,aaronasterling告诉我
cmd = "attach \"%s\" as toMerge" % "b.db" : 是错的cmd = 'attach "{0}" as toMerge'.format("b.db") : 是正确的cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) :是对的但是,我认为第一和第二是相同的.这三者有什么不同?
通过man我找到
printf("%*d", width, num);
Run Code Online (Sandbox Code Playgroud)
和
printf("%2$*1$d", width, num);
Run Code Online (Sandbox Code Playgroud)
是等价的.
但IMO的第二种风格应该是:
printf("%*d", num, width);
Run Code Online (Sandbox Code Playgroud)
但通过测试似乎man是正确的; 为什么?
在python中将变量[i]引入字符串中.
例如,查看以下脚本,我只想为图像命名,例如geo [0].Tiff ...到geo [i].tiff,或者如果你使用会计,因为我可以替换价值链的一部分来生成一个计数器.
data = self.cmd("r.out.gdal in=rdata out=geo.tif")
self.dataOutTIF.setValue("geo.tif")
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答
我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …Run Code Online (Sandbox Code Playgroud) str = 'I love %s and %s, he loves %s and %s.'
Run Code Online (Sandbox Code Playgroud)
我想用这种格式来显示
我喜欢苹果和音调,他喜欢苹果和音调.
只需添加两个变量,但需要一种方法在一个句子中使用它两次.