小编Ari*_*dni的帖子

为什么在不指定关键字start时枚举执行速度较慢?

enumerate使用start指定的默认参数进行计时时,我注意到以下奇怪的行为:

In [23]: %timeit enumerate([1, 2, 3, 4])
The slowest run took 7.18 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 511 ns per loop

In [24]: %timeit enumerate([1, 2, 3, 4], start=0)
The slowest run took 12.45 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 1.22 µs per loop
Run Code Online (Sandbox Code Playgroud)

因此,对于start …

python enumerate python-2.7 python-3.x python-internals

10
推荐指数
1
解决办法
287
查看次数

如何解压深嵌套的可迭代结构

比方说,我有一个包含许多子元素的结构,其中一些子结构是结构:

v = [1, 2, 3, [4, (5, 6)]]
Run Code Online (Sandbox Code Playgroud)

如何将这些解包为一系列仅包含结构内容而不包含结构的名称?

尝试使用星号表达式a, b, c, d, e, f = v引发一段ValueError时间会为名称分配结构.我怎样才能解压缩它们以获得:

print(a, b, c, d, e, f)
Run Code Online (Sandbox Code Playgroud)

打印:

1 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)

python iterable python-3.x iterable-unpacking

4
推荐指数
1
解决办法
344
查看次数