Iterables¶
manimlib/utils/iterables.py
这个文件中主要实现了和列表字典处理有关的函数
- manimlib.utils.iterables.remove_list_redundancies(l: Sequence[T]) list[T] ¶
Used instead of list(set(l)) to maintain order Keeps the last occurrence of each element
对列表 l 去重,保留重复元素最后一次出现
- manimlib.utils.iterables.list_update(l1: Iterable[T], l2: Iterable[T]) list[T] ¶
Used instead of list(set(l1).update(l2)) to maintain order, making sure duplicates are removed from l1, not l2.
从 l1 中删除 l2 中重复项,再与 l2 合并
- manimlib.utils.iterables.list_difference_update(l1: Iterable[T], l2: Iterable[T]) list[T] ¶
返回两列表中不同的项的列表
- manimlib.utils.iterables.adjacent_n_tuples(objects: Sequence[T], n: int) zip[tuple[T, T]] ¶
objects 的相邻 n 元组(返回zip)
- manimlib.utils.iterables.adjacent_pairs(objects: Sequence[T]) zip[tuple[T, T]] ¶
objects 相邻对
- manimlib.utils.iterables.batch_by_property(items: Iterable[T], property_func: Callable[[T], S]) list[tuple[T, S]] ¶
Takes in a list, and returns a list of tuples, (batch, prop) such that all items in a batch have the same output when put into property_func, and such that chaining all these batches together would give the original list (i.e. order is preserved)
- manimlib.utils.iterables.listify(obj: object) list ¶
根据 obj 返回列表
若 obj 为 str 类型,返回 [obj]
尝试返回 list(obj)
若报错,返回 [obj]
- manimlib.utils.iterables.resize_array(nparray: ndarray, length: int) ndarray ¶
重置数组长度
若长度变短,则截断为前半部分元素
若长度不变,则直接返回
若长度变长,则循环拷贝
- manimlib.utils.iterables.resize_preserving_order(nparray: ndarray, length: int) ndarray ¶
重置数组长度,但保留原有的序列
若长度为 0,则返回 0 数组
若长度变短,则截断为前半部分元素
若长度不变,则直接返回
若长度变长,则类似 [1,2,3] 变为 [1,1,2,2,3,3]
- manimlib.utils.iterables.resize_with_interpolation(nparray: ndarray, length: int) ndarray ¶
重置数组长度,并对中间元素进行线性插值,插值起止点分别为数组的首尾元素
- manimlib.utils.iterables.make_even(iterable_1: Sequence[T], iterable_2: Sequence[S]) tuple[list[T], list[S]] ¶
将 iterable_1 和 iterable_2 调成一样的长度,不足的伸缩调整
- manimlib.utils.iterables.hash_obj(obj: object) int ¶
获取 object 的哈希值