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

查看:113
本文介绍了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,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和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天全站免登陆