2017-02-23 14 views
2

の複数のネストされたリストに参加:私はそれからに行くとどのように変換と私はリストの次のネストされたリストを持っているリスト

a = [[[[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
    [-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.33], [-79.43395535750368, -1.692093535418117], 
    [-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
    [-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758]]], [[[-79.43381375958064, -1.691845715849684], 
    [-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
    [-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
    [-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]]] 

を:

a = [[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.33], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758], [-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]] 

ので、基本的には、ネストされたリストを削除し、各リストの先頭と最後に二重括弧を付けます。私は大成功せず、次のとittertoolsを試してみた:

flatten = lambda list: [item for sublist in list for item in sublist] 

注:LEN(A)== 2つの

多くの感謝!

[z for x in a for y in x for z in y] 

をし、それを証明するために:あなたが試すことができ

+3

もし特定の場合には、 '[0] [0]'? –

+3

@CarlesMitjans、@Arman、 'len(a)== 2' – falsetru

+1

@falsetruあなたは正しく、通知していませんでした –

答えて

2

これは仕事です。

a = a[0][0] + a[1][0] 

これは次のように拡張することができ、

a = sum([a[i][0] for i in range(len(a))], []) 
+0

これは私にとって最も賢明な解決策に思えます。それは、2つ以上の要素が容易に存在する場合にも機能するように適合させることができる。 –

+0

正確に!フォーマットが一貫している場合は、これが最も簡単な方法です。 @sphericalcowboy – frederick99

+0

皆さんはダウン投票の理由をコメントできますか?これは「間違った」答えではありません。 – frederick99

0

len([y for x in a for y in x]) == 21 

あなたはpprintを使用して、適切にそれを見ることができます。

pprint.pprint(a) 

出力リレー:

[[[[-79.43402638260521, -1.69184588855758], 
    [-79.4339722432865, -1.691845844583909], 
    [-79.43397178076256, -1.691851284533779], 
    [-79.43395283944169, -1.692053292637794], 
    [-79.43395281911414, -1.33], 
    [-79.43395535750368, -1.692093535418117], 
    [-79.43390444734398, -1.69223087834723], 
    [-79.43390428016939, -1.692231372437897], 
    [-79.43374523144152, -1.692750043925838], 
    [-79.4340256570161, -1.692750271834557], 
    [-79.43402638260521, -1.69184588855758]]], 
[[[-79.43381375958064, -1.691845715849684], 
    [-79.43312765678151, -1.691845158387183], 
    [-79.4331269307764, -1.692749541273626], 
    [-79.43354270912953, -1.692749879305633], 
    [-79.43364983051107, -1.692588468489809], 
    [-79.4336510738479, -1.692585646334773], 
    [-79.43371548446397, -1.692327269168548], 
    [-79.43380554258165, -1.692094789340216], 
    [-79.43380615195998, -1.692091785860122], 
    [-79.43381375958064, -1.691845715849684]]]] 

pprint.pprint([z for x in a for y in x for z in y]) 

出力:

[[-79.43402638260521, -1.69184588855758], 
[-79.4339722432865, -1.691845844583909], 
[-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], 
[-79.43395281911414, -1.33], 
[-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], 
[-79.43390428016939, -1.692231372437897], 
[-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], 
[-79.43402638260521, -1.69184588855758], 
[-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], 
[-79.4331269307764, -1.692749541273626], 
[-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], 
[-79.4336510738479, -1.692585646334773], 
[-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], 
[-79.43380615195998, -1.692091785860122], 
[-79.43381375958064, -1.691845715849684]] 
2

があなたのリストを任意にネストされていないが、あなただけの深い1つのレベルに行くためにしようとしている、あなたは

a = [item for sublist in a for subsublist in sublist for item in subsublist] 

デモのような非常にシンプルなものを選ぶことができると仮定すると、

>>> a = [[[[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.33], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758]]], [[[-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]]] 
>>> desired = [[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.33], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758], [-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]] 

>>> a = [item for sublist in a for subsublist in sublist for item in subsublist] 
>>> a == desired 
True 

しかし、falsetruで指摘したように、あなたの例では空のレベルの入れ子があります。この場合、解決策はきちんと整理できますas they have shown

0

ネストされたリストを反復処理して結合する必要があります。

簡単な方法:

def flatten1(list_of_lists): 
    "Flattens one level of lists." 
    result = [] 
    for sub_list in list_of_lists: 
    result.extend(sub_list) 
    return result 

賢い方法、あなたはリストを追加することができるという事実を利用:

flatten1 = lambda(list_of_lists): sum(list_of_lists, []) 

を今、あなたはa[0] = flatten1(a[0])をすることができます。

2

itertools.chain.from_iterableを使用する:

list(itertools.chain.from_iterable(xs[0] for xs in a)) 

以上は次のようになりますa[0][0] + a[1][0] + ...(連結挟ん原因リスト作成なし)


>>> a = [[[[-79.43402638260521, -1.69184588855758], 
...  [-79.4339722432865, -1.691845844583909], 
...  [-79.43397178076256, -1.691851284533779], 
...  [-79.43395283944169, -1.692053292637794], 
...  [-79.43395281911414, -1.33], 
...  [-79.43395535750368, -1.692093535418117], 
...  [-79.43390444734398, -1.69223087834723], 
...  [-79.43390428016939, -1.692231372437897], 
...  [-79.43374523144152, -1.692750043925838], 
...  [-79.4340256570161, -1.692750271834557], 
...  [-79.43402638260521, -1.69184588855758]]], 
...  [[[-79.43381375958064, -1.691845715849684], 
...  [-79.43312765678151, -1.691845158387183], 
...  [-79.4331269307764, -1.692749541273626], 
...  [-79.43354270912953, -1.692749879305633], 
...  [-79.43364983051107, -1.692588468489809], 
...  [-79.4336510738479, -1.692585646334773], 
...  [-79.43371548446397, -1.692327269168548], 
...  [-79.43380554258165, -1.692094789340216], 
...  [-79.43380615195998, -1.692091785860122], 
...  [-79.43381375958064, -1.691845715849684]]]] 
>>> 
>>> import itertools 
>>> import pprint 
>>> b = list(itertools.chain.from_iterable(xs[0] for xs in a)) 
>>> pprint.pprint(b) 

結果:

[[-79.43402638260521, -1.69184588855758], 
[-79.4339722432865, -1.691845844583909], 
[-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], 
[-79.43395281911414, -1.33], 
[-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], 
[-79.43390428016939, -1.692231372437897], 
[-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], 
[-79.43402638260521, -1.69184588855758], 
[-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], 
[-79.4331269307764, -1.692749541273626], 
[-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], 
[-79.4336510738479, -1.692585646334773], 
[-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], 
[-79.43380615195998, -1.692091785860122], 
[-79.43381375958064, -1.691845715849684]] 
関連する問題