<bdo id='ReG6p'></bdo><ul id='ReG6p'></ul>
      <legend id='ReG6p'><style id='ReG6p'><dir id='ReG6p'><q id='ReG6p'></q></dir></style></legend>
      <i id='ReG6p'><tr id='ReG6p'><dt id='ReG6p'><q id='ReG6p'><span id='ReG6p'><b id='ReG6p'><form id='ReG6p'><ins id='ReG6p'></ins><ul id='ReG6p'></ul><sub id='ReG6p'></sub></form><legend id='ReG6p'></legend><bdo id='ReG6p'><pre id='ReG6p'><center id='ReG6p'></center></pre></bdo></b><th id='ReG6p'></th></span></q></dt></tr></i><div id='ReG6p'><tfoot id='ReG6p'></tfoot><dl id='ReG6p'><fieldset id='ReG6p'></fieldset></dl></div>

    1. <tfoot id='ReG6p'></tfoot>
    2. <small id='ReG6p'></small><noframes id='ReG6p'>

    3. Python,比较子列表和制作列表

      Python, comparison sublists and making a list(Python,比较子列表和制作列表)
        <tbody id='Fvrqz'></tbody>
        <bdo id='Fvrqz'></bdo><ul id='Fvrqz'></ul>

      • <tfoot id='Fvrqz'></tfoot>
          <legend id='Fvrqz'><style id='Fvrqz'><dir id='Fvrqz'><q id='Fvrqz'></q></dir></style></legend>

            <small id='Fvrqz'></small><noframes id='Fvrqz'>

              • <i id='Fvrqz'><tr id='Fvrqz'><dt id='Fvrqz'><q id='Fvrqz'><span id='Fvrqz'><b id='Fvrqz'><form id='Fvrqz'><ins id='Fvrqz'></ins><ul id='Fvrqz'></ul><sub id='Fvrqz'></sub></form><legend id='Fvrqz'></legend><bdo id='Fvrqz'><pre id='Fvrqz'><center id='Fvrqz'></center></pre></bdo></b><th id='Fvrqz'></th></span></q></dt></tr></i><div id='Fvrqz'><tfoot id='Fvrqz'></tfoot><dl id='Fvrqz'><fieldset id='Fvrqz'></fieldset></dl></div>

              • 本文介绍了Python,比较子列表和制作列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个包含很多子列表的列表.即

                I have a list that contains a lot of sublists. i.e.

                mylst = [[1, 343, 407, 433, 27], 
                         [1, 344, 413, 744, 302], 
                         [1, 344, 500, 600, 100], 
                         [1, 344, 752, 1114, 363], 
                         [1, 345, 755, 922, 168], 
                         [2, 345, 188, 1093, 906], 
                         [2, 346, 4, 950, 947], 
                         [2, 346, 953, 995, 43], 
                         [3, 346, 967, 1084, 118], 
                         [3, 347, 4, 951, 948], 
                         [3, 347, 1053, 1086, 34], 
                         [3, 349, 1049, 1125, 77], 
                         [3, 349, 1004, 1124, 120], 
                         [3, 350, 185, 986, 802], 
                         [3, 352, 1018, 1055, 38]]
                

                我想先对这个列表进行分类,然后通过三个步骤制作另一个列表.首先,我想在每个子列表中的第一项相同时比较子列表,即 mylist[a][0]==1.其次,比较子列表中的第二项,如果子列表中的第二项与以下子列表中的另一个第二项之间的差异小于2,则计算第三项或第四项之间的差异.如果第三项和第四项的差异小于 10,那么我想附加子列表的索引.

                I want to start categorizing this list firstly and making another list by using three steps. First of all, I want to compare sublists when the first item in each sublist is the same, i.e mylist[a][0]==1. Secondly, comparing second item in sublists, and if difference between the second item in the sublist and another second item in the following sulbists under 2, then calculate the difference between third items or fourth items. If either of the difference for third and fourth item is under 10, then I want to append index of the sublist.

                我想要的结果应该是……像这样:[0, 1, 3, 4, 6, 7, 10, 11, 12]

                The result that I want should be... like this : [0, 1, 3, 4, 6, 7, 10, 11, 12]

                以下是我的天真尝试.

                以下是我的天真尝试.

                def seg(mylist) :
                    Segments = []
                    for a in range(len(mylist)-1) :
                        for index, value in enumerate (mylist) :
                            if mylist[a][0] == 1 :
                                if abs(mylist[a][1] - mylist[a+1][1]) <= 2 :
                                    if (abs(mylist[a][2] - mylist[a+1][2]) <= 10 or 
                                        abs(mylist[a][3] - mylist[a+1][3]) <= 10) :
                                        Segments.append(index)
                return Segments
                

                def seg(mylist) :
                    Segments= []
                    for index, value in enumerate(mylist) :
                        for a in range(len(mylist)-1) :
                            if mylist[a][0] == 1 :
                                try :
                                    if abs(mylist[a][1]-mylist[a+1][1]) <= 2 :
                                        if (abs(mylist[a][2]-mylist[a+1][2]) <= 10 or
                                            abs(mylist[a][3] - mylist[a+1][3]) <= 10) :
                                            Segments.append(index)
                                except IndexError :
                                    if abs(mylist[a][1]-mylist[a+1][1]) <= 2 :
                                        if (abs(mylist[a][2]-mylist[a+1][2]) <= 10 or
                                            abs(mylist[a][3] - mylist[a+1][3]) <= 10):
                                            Segments.append(index)
                return Segments
                

                这些代码看起来一点也不好看,结果也没有按照我的预期显示.在底部,我写了 try and except 来处理索引错误(列表超出范围),最初我使用 'while' 迭代而不是 'for' 迭代.

                These codes don't look nice at all, and result are not showing as that I intended to. In the bottom one, I wrote try and except to handle index error(list out of range), initially I used 'while' iteration instead of 'for' iteration.

                我应该怎么做才能得到我想要的结果?我怎样才能纠正这些代码看起来更像'pythonic'的方式?任何想法对我来说都很棒,提前非常感谢.

                What should I do to get result that I wanted to? How can I correct those codes to look like more 'pythonic' way? Any idea would be great for me, and many thanks in advance.

                推荐答案

                您将不得不捕获重复的索引,但这应该更有效:

                You will have to catch the duplicate indexes but this should be a lot more efficient:

                gr = []
                it = iter(mylst)
                prev = next(it)
                
                for ind, ele in enumerate(it):
                    if ele[0] == prev[0] and abs(ele[1] - prev[1]) <= 2:
                        if any(abs(ele[i] - prev[i]) < 10 for i in (2, 3)):
                            gr.extend((ind, ind+1))
                    prev = ele
                

                根据您的逻辑,不应出现 6 和 7,因为它们不符合条件:

                Based on your logic 6 and 7 should not appear as they don't meet the criteria:

                     [2, 346, 953, 995, 43], 
                     [3, 346, 967, 1084, 118], 
                

                同样为 10 出现它应该是 <= 2 而不是 <2 根据您的描述.

                Also for 10 to appear it should be <= 2 not < 2 as per your description.

                您可以使用 OrderedDict 删除欺骗并保持顺序:

                You could use an OrderedDict to remove the dupes and keep the order:

                from collections import OrderedDict
                
                print(OrderedDict.fromkeys(gr).keys())
                [0, 1, 3, 4, 10, 11, 12]
                

                这篇关于Python,比较子列表和制作列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  1. <tfoot id='zOWDx'></tfoot>
                      <tbody id='zOWDx'></tbody>

                    <i id='zOWDx'><tr id='zOWDx'><dt id='zOWDx'><q id='zOWDx'><span id='zOWDx'><b id='zOWDx'><form id='zOWDx'><ins id='zOWDx'></ins><ul id='zOWDx'></ul><sub id='zOWDx'></sub></form><legend id='zOWDx'></legend><bdo id='zOWDx'><pre id='zOWDx'><center id='zOWDx'></center></pre></bdo></b><th id='zOWDx'></th></span></q></dt></tr></i><div id='zOWDx'><tfoot id='zOWDx'></tfoot><dl id='zOWDx'><fieldset id='zOWDx'></fieldset></dl></div>

                      <small id='zOWDx'></small><noframes id='zOWDx'>

                        <legend id='zOWDx'><style id='zOWDx'><dir id='zOWDx'><q id='zOWDx'></q></dir></style></legend>
                          <bdo id='zOWDx'></bdo><ul id='zOWDx'></ul>