我有一個這樣的陣列串列:
我有一個這樣的陣列串列。
list = [1, 2,3, 4, 9,1,12。 9,8,7, 8, 9,10,12。 16,1,2, 3, 4,5,6, 7, 8,9,10]
我想從一個有序的數字串列中創建一個子串列(這些數字在arr_list中已經有了順序,例如1,2,3,4已經有了順序,類似的還有7,8,9,10和最后10個數字)
最終的輸出將是這樣的:
[[1, 2, 3, 4],[7,8,9,10] 。 [1,2,3,4,5。 6,7,8,9,10】]
嘗試比較第一個for回圈的i'th元素是否小于第二個for回圈的j'th元素。
這是我所嘗試的:
這是我所嘗試的。
sub_list=[] 。
for i in range(0, length_of_list):
for j in range(i 1, length_of_list):
if(arr_list[i] < arr_list[j])。
sub_list.append(arr_list[i])
else:
pass.
初次接觸python,非常感謝任何線索。
uj5u.com熱心網友回復:
試試這個...
res, temp = [], [] 。
lst = [1,2,3,4,9, 1, 12,9,8,7,8,9, 10,12,16,1,2, 3, 4,5,6, 7, 8,9,10]
for i in lst:
if (not temp) or (temp[-1] == i-1) 。
temp.append(i)
else:
res.append(temp)
temp = [i]
res.append(temp)
print([i for i in res if len(i)> 1])
輸出:
[[1, 2, 3, 4], [7, 8, 9, 10] 。[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ]
告訴我,如果它對你來說是好的......
。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/334245.html
標籤:
