转换&将数组列表格式化为元组

时间:2017-06-12 12:44:45

标签: python python-2.7

我有一个如下所示的数组列表:

c = [
 [
  [
    [670515.231, 6577538.931, 51.703], 
    [670486.853, 6577531.536, 51.703], 
    [670488.927, 6577523.577, 51.703], 
    [670517.305, 6577530.971, 51.703], 
    [670515.231, 6577538.931, 51.703]
  ]
 ]
]

我想将它转换/格式化为数组的元组:s:

new_c =(
    [670515.231, 6577538.931, 51.703], 
    [670486.853, 6577531.536, 51.703], 
    [670488.927, 6577523.577, 51.703], 
    [670517.305, 6577530.971, 51.703], 
    [670515.231, 6577538.931, 51.703])

最好的方法是什么?

2 个答案:

答案 0 :(得分:2)

>>> c = [
...  [
...   [
...     [670515.231, 6577538.931, 51.703],
...     [670486.853, 6577531.536, 51.703],
...     [670488.927, 6577523.577, 51.703],
...     [670517.305, 6577530.971, 51.703],
...     [670515.231, 6577538.931, 51.703]
...   ]
...  ]
... ]

>>> new_c = tuple(c[0][0])

>>> pprint(new_c)
([670515.231, 6577538.931, 51.703],
 [670486.853, 6577531.536, 51.703],
 [670488.927, 6577523.577, 51.703],
 [670517.305, 6577530.971, 51.703],
 [670515.231, 6577538.931, 51.703])

答案 1 :(得分:1)

你可以这样做:

c_tuple = tuple(c[0][0])

或者,如果列表的嵌套深度对于每种情况都不同:

tmp = c
while len(tmp) == 1:
    tmp = tmp[0]

c_tuple = tuple(tmp)