2017-06-12 73 views
-5

我有陣列的列表看起來像這樣:轉換和格式列表,元組

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]) 

這樣做的最佳方法是什麼?

+3

你想'元組(C [0] [0] )'? – ozgur

+0

只是爲了澄清:從技術上講,這些列表不是數組。 :) – MSeifert

回答

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

你可以這樣做:

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

如果列表中的嵌套深度是每個情況不同:

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

c_tuple = tuple(tmp)