python - 在优化方法中将 numpy ndarray 转换为元组的元组

标签 python numpy

我有 numpy ndarray,opencv findContours() 的结果。
我想有效地将​​结果的每个元素从 numpy 数组转换为元组的元组。
尝试了 tolist()、asarray() 等,但它们都没有给我确切的结果。

示例

numpy 数组:

    [[[191 307]]

     [[190 308]]

     [[181 308]]]

元组的元组:

((191,307),(190,308),(181,308))

更新
元组(元素[0])返回

(array([[191 ,307]], dtype=int32),array([[190, 308]], dtype=int32),array([[181,308]], dtype=int32))

最佳答案

In [9]: a = numpy.array([[[191, 307]],
   ...:                  [[190, 308]],
   ...:                  [[181, 308]]])

In [10]: tuple(tuple(row[0]) for row in a)
Out[10]: ((191, 307), (190, 308), (181, 308))

关于python - 在优化方法中将 numpy ndarray 转换为元组的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51877618/

相关文章:

python - numpy.bincounts 的反转?

python - 在python或numpy或pandas中,如何将整数分配给字符串

python - 当 python 出现内存错误时,如何读取文件中的有限行

python - 按总和条件分组

python - 在cloudcontrol上,如何进行URL重写?

python - 在 numpy 中转换一组数字,以便每个数字都转换为其他一些小于它的数字

python——for循环的理解

python - Allen Downey 的 Think Python 第 12 章(元组)的练习 6

python - 测试numpy数组中的所有值是否相等

python-3.x - 语音事件检测