“Python”的版本间的差异

来自wrc's Wiki
跳到导航 跳到搜索
第4行: 第4行:
  
 
* <code>len(a_list)</code> 复杂度为 O(1)
 
* <code>len(a_list)</code> 复杂度为 O(1)
 +
* <code>len(a_str)</code> 复杂度为 O(1)
 +
*: 使用 {{code|python|2=python -m timeit -s 's = "abcde"*10000' 'len(s)'}} 测试,更改 <code>s</code> 的长度
  
 
== 例子 ==
 
== 例子 ==

2021年6月3日 (四) 05:46的版本

笔记

复杂度

  • len(a_list) 复杂度为 O(1)
  • len(a_str) 复杂度为 O(1)
    使用 python -m timeit -s 's = "abcde"*10000' 'len(s)' 测试,更改 s 的长度

例子

实现 Rust 中的 Iterator::skip

忽略一个迭代器的前 n 个元素。

[1]

itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])
from itertools import islice

islice(iterator, n, None)

类的静态方法

class Example:
    @classmethod
    def load(cls, filepath):
        obj = cls.__new__(cls)
        obj.foo = somefunc(filepath)
        return obj

外部链接

参考资料