相关疑难解决方法(0)

反引号对python解释器意味着什么:`num`

我正在玩列表推导,我在另一个网站上看到了这个小片段:

return ''.join([`num` for num in xrange(loop_count)])
Run Code Online (Sandbox Code Playgroud)

我花了几分钟试图复制这个函数(通过输入),然后才意识到`num`它正在破坏它.

在这些字符中包含语句的内容是什么?从我所看到的它相当于str(num).但是当我计时:

return ''.join([str(num) for num in xrange(10000000)])
Run Code Online (Sandbox Code Playgroud)

它需要4.09s而:

return ''.join([`num` for num in xrange(10000000)])
Run Code Online (Sandbox Code Playgroud)

需要2.43秒.

两者都给出了相同的结果,但其中一个慢得多.这里发生了什么?

编辑:奇怪... repr()给出稍微慢一点的结果`num`.2.99s vs 2.43s.使用Python 2.6(尚未尝试过3.0).

python list-comprehension

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

通过python脚本截取屏幕截图.[Linux的]

我想通过python脚本截取屏幕截图并且不引人注意地保存它.

我只对Linux解决方案感兴趣,应该支持任何基于X的环境.

python linux screenshot

78
推荐指数
8
解决办法
9万
查看次数

如何连接str和int对象?

如果我尝试执行以下操作:

things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)

我在Python 3.x中收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)

......以及Python 2.x中的类似错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

python string concatenation python-2.x python-3.x

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

在Python中用字符串和整数创建一个字符串

我尝试取一个整数并在其前面添加"b",将其转换为字符串时出现此错误:

  File "program.py", line 19, in getname
    name = "b" + num
TypeError: Can't convert 'int' object to str implicitly
Run Code Online (Sandbox Code Playgroud)

这与此功能有关:

num = random.randint(1,25)
name = "b" + num
Run Code Online (Sandbox Code Playgroud)

python string random integer python-3.x

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

在Python 2.x中,使用反引号从int对象获取十进制字符串是可怕的吗?

在Python 2.x中,使用反引号从int对象获取十进制字符串是可怕的吗?因为反推是repr(),不是str()吗?当我回答这个问题时,我注意到了.

在Python源代码中,它们在Python源代码中具有相同的功能,即intobject.c

(reprfunc)int_to_decimal_string,            /* tp_repr */
....
(reprfunc)int_to_decimal_string,            /* tp_str */
Run Code Online (Sandbox Code Playgroud)

你怎么看?

python python-2.x backticks

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

Python 字符串与整数之和的串联

我想打印数字和一些字符串,例如:

print("root: " + rootLeaf + " left:" + leftLeaf + "sum: " +(rootLeaf+leftLeaf) )
Run Code Online (Sandbox Code Playgroud)

这里“root”,“left”和“sum”是字符串,其中rootLeaf和leftleaf是整数,我想找到它们的总和。

在这里检查了帖子,但无法实现整数之和(字符串连接中的数学运算)

python printing string sum concatenation

0
推荐指数
1
解决办法
7702
查看次数