如何根据指定元素(0.0,0.0)将Android生成的坐标列表拆分为多个子列表
Python坐标列表拆分方案
实现逻辑
遍历全量坐标列表,每次遇到起始标记(0.0, 0.0)时新建一个子列表,后续坐标全部追加到当前最新的子列表中,拆分后的子列表数量和应用运行次数完全一致。
实现代码
基础版本(适用于列表首元素为(0.0, 0.0)的正规导出数据)
def split_coordinate_list(big_list): result = [] for coord in big_list: if coord == (0.0, 0.0): result.append([]) result[-1].append(coord) return result
容错版本(兼容开头存在无效脏数据的情况)
def split_coordinate_list(big_list): result = [] for coord in big_list: if coord == (0.0, 0.0): result.append([]) if result: result[-1].append(coord) 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)] split_lists = split_coordinate_list(big_list) # 输出结果对应你需要的三个子列表 print(split_lists[0]) # 对应list1 print(split_lists[1]) # 对应list2 print(split_lists[2]) # 对应list3
内容的提问来源于stack exchange,提问作者Filipe Fontes
相关产品推荐
相关产品推荐

