2017-02-07 84 views
0

我想多個列表合併成一個列表如何將多個列表合併成一個列表在Python 3.5?

e.g:['of', 'participants:', '25']['participants:', '25', 'we']['25', 'we', 'require']

代碼之後: 「['of', 'participants:', '25'],['participants:', '25', 'we'],['25', 'we', 'require']

for wrd in d: 
    child=child.lower() 
    child=child.replace(' ','') 
    dat =child 
    r = re.compile(dat) 
    s=str(wrd) 
    test=[sentence in dat for sentence in s] 
    if r.search(s) and any(test): 
     print(s) 

回答

0

嗯再次聲明,我並不完全相信你在做什麼。用你的正則表達式來看你的代碼。也許用字典會更好。如果你必須使用一個列表,你可以像循環一樣將它合併到一個元組中。讓我知道它是否有幫助。

>>> list_one = ['of', 'participants:', '25'] 
>>> list_two = ['participants:', '25', 'we'] 
>>> list_three = ['25', 'we', 'require'] 
>>> thing =() 
>>> thing += tuple([list_one]) 
>>> thing += tuple([list_two]) 
>>> thing += tuple([list_three]) 
>>> thing 
(['of', 'participants:', '25'], ['participants:', '25', 'we'], ['25', 'we', 'require']) 
>>> for i in range(len(thing)): 
...  print thing[i] 
... 
['of', 'participants:', '25'] 
['participants:', '25', 'we'] 
['25', 'we', 'require'] 
+0

S = STR(WRD) 測試= [句子中的DAT中代表S句子]如果r.search(S)和任何(測試) : 打印(S)....我很越來越[ '的', '參與者:', '25'] [ '參與者:', '25', '我們'] [ '25', '我們', '需要'],但我需要輸出爲[ '中', '參與者:', '25'], [ '參與者:', '25', '我們'], [ '25', '我們', '需要'] –

+0

讓我知道這是否幫助我認爲是你的要求 – wetw0rk

+0

那麼listOne = [ '的', '參與者:', '25'] [ '參與者:', '25', '我們'] [ '25', '我們', '需要' ]這是我得到的 –

相關問題