簡體   English   中英

如何將列表中的元素組合到新的嵌套列表?

[英]How to combine elements in list to a new nested list?

我有一個像這樣的列表'r':

[["", 1], ["this is a text line", 2], ["this is a text line", 3], ["this is a text line", 4], ["", 5], ["", 6], ["this is a text line", 7],["this is a text line", 8], ["this is a text line", 9], ["this is a text line", 10], ["", 11], ["this is a text line", 12], ["this is a text line", 13], ["this is a text line", 14], ["", 15], ["this is a text line", 16], ["this is a text line", 17], ["this is a text line", 18], ["", 19]]

要知道我的空行和文本行在哪里我過濾我的列表:

empty = [x[1] for x in r if regex.search("^\s*$", x[0])]
text = [x[1] for x in r if regex.search("\S", x[0])]

輸出:

empty = [1, 5, 6, 11, 15, 19]
text= [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]

我想要做的是在文本中按順序組合數字(text [i] -text [i + 1])= +1(為了定義段落):

finaltext = [[2, 3, 4], [7, 8, 9, 10], [12, 13, 14], [16, 17, 18]]
finaltext including empty = [[2, 3, 4, 5, 6], [7, 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]

如何根據條件對列表中的元素進行分組?

使用itertools.groupby

from itertools import groupby, zip_longest
grp_list = [list(g) for k,g in groupby(r, lambda x:x[0]=='')]
grp_list = grp_list[1:] if r[0][0] == '' else grp_list
text = [[j[1] for j in i] for i in grp_list]

finaltext = text[::2]
print (finaltext)
#[[2, 3, 4], [7, 8, 9, 10], [12, 13, 14], [16, 17, 18]]

finaltext_including_empty = [i+j for i,j in zip_longest(text[::2], text[1::2], fillvalue=[])]
print (finaltext_including_empty)
#[[2, 3, 4, 5, 6], [7, 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]

groupby根據條件將列表分組為子列表塊,這里是lambda x:x[0]=='' ,意味着創建一個列表塊直到你看到一個空字符串,並按照這個規則一直到下面結束如下

[[['', 1]], [['this is a text line', 2], ['this is a text line', 3], ['this is a text line', 4]], [['', 5], ['', 6]],........]

pip install more_itertools

from more_itertools import chunked

empty = [1, 5, 6, 11, 15, 19]
text= [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]

finaltext_ = sorted(empty + text)

list(chunked(finaltext_,4))
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19]]

沒有任何modules純Python解決方案:

這可以使用諸如numpygroupby modules來完成,但我認為沒有它們就會調用它,只需要使用普通的Python 這是我的解決方案:

text = [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]
s = 0
finaltext = []
for i in range(len(text)-1):
    if text[i] + 1 != text[i+1]:
        finaltext.append(text[s:i+1])
        s = i+1

finaltext.append(text[s:])

最終finaltext為:

[[2, 3, 4], [7, 8, 9, 10], [12, 13, 14], [16, 17, 18]]

更新

要獲得這兩個lists (不確定為什么要這樣),您可以使用以下內容:

empty = [1, 5, 6, 11, 15, 19]
text = [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]
s = 0
finaltext = []
finaltext_including_empty = []
for i in range(len(text)-1):
    if text[i] + 1 != text[i+1]:
        finaltext.append(text[s:i+1])
        finaltext_including_empty.append(list(range(text[s], text[i+1])))
        s = i+1

finaltext.append(text[s:])
finaltext_including_empty.append(list(range(text[s],max(empty[-1]+1, text[-1]+1))))

最終finaltext與之前相同, finaltext_including_empty為:

[[2, 3, 4, 5, 6], [7, 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM