简体   繁体   English

多维数组到多维元组

[英]Multidimensional array to multidimensional tuple

If I have an array such: 如果我有一个这样的数组:

inner_loop = 
[
 [
  [18, 119], [42, 119], [42, 95], [18, 95]
 ],
 [
  [80, 96], [80, 75], [59, 75], [59, 96]
 ]
]

how do I convert it to a multidimensional tuple like 我如何将其转换为多维元组

(
 (
  (18, 119), (42, 119), (42, 95), (18, 95)
 ),
 (
  (80, 96), (80, 75), (59, 75), (59, 96)
 )
)

I have tried: 我努力了:

tuple(tuple(y) for y in (tuple(tuple (x) for x in (tuple(inner_loops)))))

but the last level is not converted. 但最后一级没有转换。

tuple(tuple(tuple(l2) for l2 in l1) for l1 in inner_loop)

Using map is a bit more readable and actually more efficient if you have large amounts of data: 如果您有大量数据,使用map可读性更高,实际上更有效:

inner_loop = tuple(tuple(map(tuple, sub)) for sub in inner_loop)

print(inner_loop)
(((18, 119), (42, 119), (42, 95), (18, 95)), ((80, 96), (80, 75), (59, 75), (59, 96)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM