[英]Split list with multiple list according with a specific element
我有一個由 android 應用程序生成的坐標列表。 我想要的是每次找到元素(0.0,0.0)時自動將列表拆分到另一個列表中,當它找到下一個(0.0,0.0)時,它將把它拆分到另一個列表中,依此類推。 元素的數量(0.0,0.0)取決於我運行應用程序的次數,因此在示例中,我運行了應用程序 3 次,但如果我運行了 4 次,我需要將 big_list 拆分為 4 個列表。 這樣,決定拆分次數的 big_list 的大小和 (0.0,0.0) 元素的數量取決於我在從數據庫中導出文件並將其加載到 python 以進行分析之前運行應用程序的次數.
這是示例:
big_list = [**(0.0, 0.0)**, (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564), **(0.0, 0.0)**, (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066), **(0.0, 0.0)**, (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]
我想要的是 3 個以元素 (0.0,0.0) 和以下元素開頭的列表,直到找到下一個 (0.0,0.0) 再次拆分:
list1 = [(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564)]
list2= [(0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066)]
list3 = [(0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]
這個方法會給你一個你需要的答案
def split_list(input_list:list) -> list:
start_ind=0
result = []
for ind, x in enumerate(input_list):
if ( x == (0.0, 0.0) or ind == len(input_list)-1 ) and ind != 0:
result.append(input_list[start_ind:ind])
start_ind=ind
return result
我同意@conol 的回答,但您可以添加這個額外的步驟並獲得您想要的結果。
基本上,我們在您編寫的分隔符上拆分列表,然后我們使用代碼更新語言環境,因此您將在運行時獲得動態創建的變量
def split_list(input_list:list) -> list:
start_ind=0
dict = {}
varname= 'list'
counter = 0
for ind, x in enumerate(input_list):
if ( x == (0.0, 0.0) or ind == len(input_list)-1 ) and ind != 0:
dict[varname+str(counter)] = input_list[start_ind:ind]
start_ind=ind
counter+=1
return dict
locals().update(split_list(a))
print(list0,list1,list2)
例如,可以定義一個 function 循環遍歷大列表的條目並創建子列表(帶有類型注釋):
from pprint import pprint
from typing import List, Optional, Tuple
def split_big_list(
big_list: List[Tuple[float, float]],
) -> List[List[Tuple[float, float]]]:
result: List[List[Tuple[float, float]]] = []
current_list: Optional[List[Tuple[float, float]]] = None
for entry in big_list:
if entry == (0.0, 0.0):
current_list = [entry]
result.append(current_list)
elif current_list is None:
# ignore entries before first tuple with zeros
continue
else:
current_list.append(entry)
return result
BIG_LIST = [(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564), (0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066), (0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]
lists = split_big_list(BIG_LIST)
pprint(lists)
結果:
[[(0.0, 0.0),
(0.7061503529548645, -0.5579889416694641),
(1.412300705909729, -1.1159778833389282),
(2.1184511184692383, -1.673966884613037),
(2.824601411819458, -2.2319557666778564)],
[(0.0, 0.0),
(0.6936703324317932, -0.573429524898529),
(1.3873406648635864, -1.146859049797058),
(2.0810110569000244, -1.7202885150909424),
(2.7677958011627197, -2.3019471168518066)],
[(0.0, 0.0),
(0.6973708868026733, -0.5689234137535095),
(1.3947417736053467, -1.137846827507019),
(2.0921125411987305, -1.7067701816558838),
(2.7894835472106934, -2.275693655014038),
(3.4868545532226562, -2.8446171283721924),
(4.184225559234619, -3.4135406017303467)]]
使用 enumerate 查找 (0,0) 項的索引。 然后 zip() 將它們與 teir 后繼者配對以形成子范圍:
starts = [i for i,v in enumerate(big_list) if v == (0,0)]
sublists = [big_list[s:e] for s,e in zip(starts,starts[1:]+[None])]
print(sublists)
[[(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564)],
[(0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066)],
[(0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]]
# if you want them in variables
# (but then you need to know how many subslits there are ahead of time):
lst1,lst2,lst3 = sublists
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.