【问题标题】:Split list with multiple list according with a specific element根据特定元素拆分具有多个列表的列表
【发布时间】:2022-01-04 18:38:27
【问题描述】:

我有一个由安卓应用生成的坐标列表。我想要的是每次找到元素(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)]

我想要的是从元素 (0.0,0.0) 和以下元素开始的 3 个列表,直到找到下一个 (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)]

【问题讨论】:

    标签: python arrays list


    【解决方案1】:

    这个方法会给你一个你需要的答案

    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
    

    【讨论】:

      【解决方案2】:

      我同意@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)
      

      【讨论】:

        【解决方案3】:

        例如,可以定义一个函数,循环遍历大列表的条目并创建子列表(带有类型注释):

        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)]]
        

        【讨论】:

          【解决方案4】:

          使用 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
          

          【讨论】:

            猜你喜欢
            • 2021-08-10
            • 2019-03-06
            • 2011-11-19
            • 2023-03-05
            • 1970-01-01
            • 2018-12-22
            • 2015-03-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多