相关疑难解决方法(0)

我可以覆盖一个namedtuple的字符串形式吗?

例如:

>>> 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)

那就是我希望字符串表示显示响度属性.这可能吗 ?

python

37
推荐指数
2
解决办法
8677
查看次数

Python中的!r和%r有什么区别?

正如标题所述,这两面旗帜有什么区别?看来他们都使用repr()将值转换为字符串?此外,在这行代码中:

"{0!r:20}".format("Hello")  
Run Code Online (Sandbox Code Playgroud)

!r前面的0是什么?

python string format string-formatting

32
推荐指数
1
解决办法
2万
查看次数

Python字符串格式:'%'比'format'函数效率更高?

我想比较不同以在Python中用不同的变量构建一个字符串:

  • 使用+来连接(被称为"加号")
  • 运用 %
  • 运用 "".join(list)
  • 使用format功能
  • 运用 "{0.<attribute>}".format(object)

我比较了3种类型的情景

  • 带2个变量的字符串
  • 4个变量的字符串
  • 带有4个变量的字符串,每个变量两次使

我每次测量100万次操作并且平均执行超过6次测量.我想出了以下时间:

计时

在每个场景中,我得出以下结论

  • 连接似乎是最快的方法之一
  • 使用格式化%比使用format函数格式化要快得多

我相信format%(例如在这个问题中)要好得多,并且%几乎被弃用了.

因此,我有几个问题:

  1. %真的比快format
  2. 如果是这样,为什么呢?
  3. 为什么"{} {}".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 performance

29
推荐指数
1
解决办法
7038
查看次数

为什么我不能使用星号表达式?

我的代码

$ 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)

python python-3.5

25
推荐指数
2
解决办法
1万
查看次数

如何使用带有python> 3.6 f-strings的gettext

以前您将使用gettext以下内容:

_('Hey {},').format(username)
Run Code Online (Sandbox Code Playgroud)

但是新的Python的f-string怎么样?

f'Hey {username}'
Run Code Online (Sandbox Code Playgroud)

python gettext f-string

18
推荐指数
2
解决办法
1314
查看次数

"%s"%格式与"{0}".format()vs"?" 格式

在这篇关于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', )) :是对的

但是,我认为第一和第二是相同的.这三者有什么不同?

python string-formatting pysqlite

15
推荐指数
2
解决办法
2万
查看次数

像"1 $"这样的位置参数如何与printf()一起使用?

通过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是正确的; 为什么?

c format printf

15
推荐指数
2
解决办法
1万
查看次数

在python中将变量值插入到字符串中

在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 gis

14
推荐指数
2
解决办法
4万
查看次数

我收到类型错误。我如何解决它?

我经常从我的 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)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

关于字符串中多个%s的python

str = 'I love %s and %s, he loves %s and %s.' 
Run Code Online (Sandbox Code Playgroud)

我想用这种格式来显示

我喜欢苹果和音调,他喜欢苹果和音调.

只需添加两个变量,但需要一种方法在一个句子中使用它两次.

python string

9
推荐指数
2
解决办法
1万
查看次数