何时使用'.flat','。flattiter'或'.flatten()'

时间:2022-10-03 15:29:46

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.

什么时候使用三个扁平化参数中的哪一个'.flat'/'。flatiter'/'。flatten'?我知道'.flat'会在数组上返回1D迭代器,这是否意味着数组保留原始形状,并且可以使用单个索引访问数组中的每个元素(例如,使用单个for循环)阵列可以是高度尺寸的)。并且'.flatten'返回原始数组的完整副本,将其展平为1D数组。

Which is less resource intensive?

哪个资源密集程度较低?

1 个解决方案

#1


3  

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.

flatiter只是flat(docs)返回的迭代器对象的类型。所以你需要知道的是它是一个像其他任何东西一样的迭代器。

Obviously, flatten consumes more memory and cpu, as it creates a new array, while flat only creates the iterator object, which is super fast.

显然,flatten消耗更多内存和cpu,因为它创建了一个新数组,而flat只创建了迭代器对象,这是超快的。

If all you need is to iterate over the array, in a flat manner, use flat.

如果您只需要以平面方式迭代数组,请使用平面。

If you need an actual flat array (for purposes other than just explicitly iterating over it), use flatten.

如果您需要一个实际的平面数组(除了显式迭代它之外的目的),请使用flatten。

#1


3  

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.

flatiter只是flat(docs)返回的迭代器对象的类型。所以你需要知道的是它是一个像其他任何东西一样的迭代器。

Obviously, flatten consumes more memory and cpu, as it creates a new array, while flat only creates the iterator object, which is super fast.

显然,flatten消耗更多内存和cpu,因为它创建了一个新数组,而flat只创建了迭代器对象,这是超快的。

If all you need is to iterate over the array, in a flat manner, use flat.

如果您只需要以平面方式迭代数组,请使用平面。

If you need an actual flat array (for purposes other than just explicitly iterating over it), use flatten.

如果您需要一个实际的平面数组(除了显式迭代它之外的目的),请使用flatten。