问题描述
何时使用三个展平参数'.flat'/'.flatiter'/'.flatten'中的哪个合适?我知道'.flat'在数组上返回一维迭代器,这是否意味着该数组保留原始形状,并且可以使用单个索引访问该数组中的每个元素(例如,即使使用单个for循环,阵列可能是高维的).然后,.flatten"将返回原始数组的完整副本,该副本将被平整为一维数组.
When is it appropriate to use which of the three flattening arguments '.flat'/'.flatiter'/'.flatten'? I know that '.flat' returns a 1D iterator over an array, does this mean that the array is left in the original shape and each element in the array can be accessed with a single index (for example using a single for loop even though the array may be highly dimensional). And '.flatten' returns a complete copy of the original array flattened out into a 1D array.
哪些资源消耗较少?
推荐答案
flatiter
只是flat
(文档).因此,您需要了解的就是它是一个与其他迭代器一样的迭代器.
flatiter
is just the type of the iterator object returned by flat
(docs). So all you need to know about it is that it's an iterator like any other.
很明显,flatten
在创建新数组时会消耗更多的内存和cpu,而flat
仅创建超快的迭代器对象.
Obviously, flatten
consumes more memory and cpu, as it creates a new array, while flat
only creates the iterator object, which is super fast.
如果只需要以扁平方式遍历数组,请使用flat
.
If all you need is to iterate over the array, in a flat manner, use flat
.
如果您需要一个实际的平面数组(除了显式地对其进行迭代以外的其他目的),请使用flatten
.
If you need an actual flat array (for purposes other than just explicitly iterating over it), use flatten
.
这篇关于何时使用'.flat','.flatiter'或'.flatten()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!