2017-04-11 55 views
1

我有一個字符串列表的列表清單,這樣的事情(代表章節,段落和文本的句子)):如何扁平化列表,而留下一些嵌套(在python)

[ [[ ['chp1p1s1'], ['chp1p1s2'], ['chp1p1s3'] ], 
    [ ['chp1p2s1'], ['chp1p2s2'], ['chp1p2s3'] ]], 
    [[ ['chp2p1s1'], ['chp2p1s2'], ['chp2p1s3'] ], 
    [ ['chp2p2s1'], ['chp2p2s2'], ['chp2p2s3'] ]] ] 

我知道如何(通過[x for y in z for x in y]例如)完全地壓平這個名單,但我希望做的是部分變平,到最後是這樣的:

[ [ ['chp1p1s1'], ['chp1p1s2'], ['chp1p1s3'], 
    ['chp1p2s1'], ['chp1p2s2'], ['chp1p2s3'] ], 
    [ ['chp2p1s1'], ['chp2p1s2'], ['chp2p1s3'], 
    ['chp2p2s1'], ['chp2p2s2'], ['chp2p2s3'] ] ] 

我設法通過一些for循環來解決這個問題:

semiflattend_list=list() 
for chapter in chapters: 
    senlist=list() 
    for paragraph in chapter: 
     for sentences in paragraph: 
      senlist.append(sentences) 
    semiflattend_list.append(senlist) 

但我想知道是否有更好,更短的解決方案? (我不認爲,zip是很長的路要走,因爲我的列表的大小不同。)

+1

你給的例子實際上是兩個不同列表的元組,我不認爲這是你的意思。你可能弄亂了括號或者逗號,但是我們需要一個可再生的例子 –

回答

1

我可以使用itertools.chain方法看到的最簡單的變化:

q = [ 
    [[ ['chp1p1s1'], ['chp1p1s2'], ['chp1p1s3'] ], 
     [ ['chp1p2s1'], ['chp1p2s2'], ['chp1p2s3'] ]], 
    [[ ['chp2p1s1'], ['chp2p1s2'], ['chp2p1s3'] ], 
     [ ['chp2p2s1'], ['chp2p2s2'], ['chp2p2s3'] ]] 
    ] 

r = [list(itertools.chain(*g)) for g in q] 
print(r) 

[[['chp1p1s1'], ['chp1p1s2'], ['chp1p1s3'], ['chp1p2s1'], ['chp1p2s2'], ['chp1p2s3']], 
[['chp2p1s1'], ['chp2p1s2'], ['chp2p1s3'], ['chp2p2s1'], ['chp2p2s2'], ['chp2p2s3']]] 

那麼,是什麼[list(itertools.chain(*g)) for g in q]的意思是:

# If I only had this 
[g for g in q] 
# I would get the same I started with. 
# What I really want is to expand the nested lists 

# * before an iterable (basically) converts the iterable into its parts. 
func foo(bar, baz): 
    print(bar + " " + baz) 

lst = ["cat", "dog"] 
foo(*lst) # prints "cat dog" 

# itertools.chain accepts an arbitrary number of lists, and then outputs 
# a generator of the results: 
c = itertools.chain([1],[2]) 
# c is now <itertools.chain object at 0x10e1fce10> 
# You don't want an generator though, you want a list. Calling `list` converts that: 
o = list(c) 
# o is now [1,2] 
# Now, together: 
myList = [[2],[3]] 
flattened = list(itertools.chain(*myList)) 
# flattened is now [2,3] 
+0

這實際上解決了它!你可能請解釋一下這個星號/圖示操作符?在[doc](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists)中,它並沒有向我解釋這可能對此有何幫助。 – dia