我最近比较了处理速度[]
和,list()
并且惊讶地发现它的[]
运行速度比它快三倍list()
.我跑了相同的测试与{}
和dict()
,结果几乎相同:[]
和{}
两个花了大约0.128sec /百万次,而list()
和dict()
大约花费每个0.428sec /万次.
为什么是这样?不要[]
和{}
(可能()
和''
,太)立即传回文字的一些空股票的份,而其明确命名同行(list()
,dict()
,tuple()
,str()
)完全去创建一个对象,他们是否真的有元素?
我不知道这两种方法有何不同,但我很想知道.我无法在文档中或在SO上找到答案,搜索空括号结果比我预期的问题更多.
我拨打了我的时序结果timeit.timeit("[]")
和timeit.timeit("list()")
,和timeit.timeit("{}")
和timeit.timeit("dict()")
,分别比较列表和字典.我正在运行Python 2.7.9.
我最近发现的" 为什么是,如果真慢于如果为1? "来比较的性能if True
来if 1
,似乎触及了类似的文字,对全局的情况; 也许值得考虑一下.
为什么这不起作用?
lambda: print "x"
Run Code Online (Sandbox Code Playgroud)
这不是一个单一的陈述,还是别的什么?关于lambda中允许的内容,文档似乎有点稀疏......
如何让Tkinter应用程序跳到前面?目前,窗口出现在我所有其他窗口后面,并没有得到焦点.
我应该打电话给我一些方法吗?
如果我在列表中不存在a.remove(x)
时调用,如何忽略"not in list"错误消息?x
a
这是我的情况:
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> a.remove(9)
Run Code Online (Sandbox Code Playgroud) 一时兴起,我最近测试了这两种方法timeit
,看看哪种评估方法更快:
import timeit
"""Test method returns True if either argument is falsey, else False."""
def and_chk((a, b)):
if not (a and b):
return True
return False
def not_or_chk((a, b)):
if not a or not b:
return True
return False
Run Code Online (Sandbox Code Playgroud)
......并得到了这些结果:
VALUES FOR a,b -> 0,0 0,1 1,0 1,1
method
and_chk(a,b) 0.95559 0.98646 0.95138 0.98788
not_or_chk(a,b) 0.96804 1.07323 0.96015 1.05874
...seconds per 1,111,111 cycles.
Run Code Online (Sandbox Code Playgroud)
效率的差异在1%到9%之间,总是有利于if not (a and b)
,这与我的预期相反,因为我理解if not a or not b
它将按顺序评估其术语(if …
python if-statement micro-optimization logical-operators python-2.7
Is there a way to test whether a variable holds a lambda
?
The context is I'd like to check a type in a unit test:
self.assertEquals(lambda, type(myVar))
Run Code Online (Sandbox Code Playgroud)
The type
seems to be "function" but I didn't see any obvious builtin type to match it.
Obviously, I could write this, but it feels clumsy:
self.assertEquals(type(lambda m: m), type(myVar))
Run Code Online (Sandbox Code Playgroud) Python的内置coerce
函数有哪些常见用途?如果根据文档我不知道type
数值,我可以看到应用它,但是还存在其他常见用法吗?我猜这也是在执行算术计算时调用的,例如.它是一个内置函数,所以可能它有一些潜在的常见用法?coerce()
x = 1.0 +2
我想在python中创建一个具有一些属性的对象,我想保护自己不会意外使用错误的属性名称.代码如下:
class MyClass( object ) :
m = None # my attribute
__slots__ = ( "m" ) # ensure that object has no _m etc
a = MyClass() # create one
a.m = "?" # here is a PROBLEM
Run Code Online (Sandbox Code Playgroud)
但运行这个简单的代码后,我得到一个非常奇怪的错误:
Traceback (most recent call last):
File "test.py", line 8, in <module>
a.m = "?"
AttributeError: 'test' object attribute 'm' is read-only
Run Code Online (Sandbox Code Playgroud)
有没有明智的程序员能够节省一些时间并让我了解"只读"错误?
假设我有一个 ,list
TruncList
其元素数量大于n
。如果我想n
从该列表的末尾删除元素,将列表重新定义为保留所需元素的自身切片(如 by TruncList = TruncList[:-n]
)或从列表中删除不需要的元素的切片(del TruncList[-n:]
如 by )更快吗?
如果我删除第一个 n
元素,答案会改变吗TruncList
,就像TruncList = TruncList[n:]
vs一样del TruncList[:n]
?
除了速度之外,这些方法中的一种是否比另一种更Pythonic?
我想重新定义方法可能会更慢,因为它会迭代TruncList
然后重新分配它,同时del
截断列表,但我不确定是否是这种情况。
我还认为del
这是更好的路线,因为这似乎是该功能的自然使用。
我有一个大NumPy.array
field_array
而小的数组match_array
,都由int
值组成.使用以下示例,如何检查match_array形状的任何段是否field_array
包含与其中的值完全对应的值match_array
?
import numpy
raw_field = ( 24, 25, 26, 27, 28, 29, 30, 31, 23, \
33, 34, 35, 36, 37, 38, 39, 40, 32, \
-39, -38, -37, -36, -35, -34, -33, -32, -40, \
-30, -29, -28, -27, -26, -25, -24, -23, -31, \
-21, -20, -19, -18, -17, -16, -15, -14, -22, \
-12, -11, -10, -9, -8, -7, -6, -5, -13, \
-3, -2, -1, …
Run Code Online (Sandbox Code Playgroud) 假设我有一个函数,我想打印出它接受的参数.我怎样才能做到这一点?
我有这个tuple
元组:
TupleOfTuples = (('Venue1', 'Name1'), ('Venue1', 'Name2'),
('Venue2', 'Name3'), ('Venue3', 'Name4'),
('Venue3', 'Name5'), ('Venue3', 'Name6'))
Run Code Online (Sandbox Code Playgroud)
我想将其转换为得到如下结果:
Output = (('Venue1', 2), ('Venue2', 1), ('Venue3', 3))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Output
包含('Venue1', 2)
,例如,在哪里发生2
的次数.'Venue1'
TupleOfTuples
我尝试len()
用来计算出现次数,但是它不起作用,因为它TupleOfTuples
不是单个元组而是元组的元组.
如何在Python2.7中完成?
Python似乎无法将数字的正确值返回到零的幂.当我给它一个文字方程时,它可以正常工作,但它总是返回正1,对于比原始数字更复杂的零到零.
以下是一些测试:
>>> -40 ** 0 # this is the correct result
-1
>>> (0 - 40) ** 0 # you'd expect this to give the same thing, but...
1
>>> a = -40 # let's try something else...
>>> a ** 0
1
>>> int(-40) ** 0 # this oughtn't to change anything, yet...
1
>>> -66.6 ** 0 # raw floats are fine.
-1.0
>>> (0 - 66.6) ** 0.0 # ...until you try and do something with …
Run Code Online (Sandbox Code Playgroud) python ×13
list ×3
python-2.7 ×3
lambda ×2
python-2.x ×2
arguments ×1
arrays ×1
built-in ×1
class ×1
count ×1
descriptor ×1
elements ×1
exception ×1
focus ×1
function ×1
if-statement ×1
literals ×1
math ×1
numpy ×1
optimization ×1
performance ×1
printing ×1
slice ×1
tkinter ×1
tuples ×1
types ×1