Python,比较子列表和制作列表 [英] Python, comparison sublists and making a list

查看:18
本文介绍了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,比较子列表和制作列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆