简体   繁体   English

如何将列表的子列表与同一列表中的其他列表的子列表进行比较

[英]How to compare sublist of list with sublist of other list that are in same list of lists

I have a list of lists with each list having sublists for example something like the following:我有一个列表列表,每个列表都有子列表,例如如下所示:

mylist = [[['AB','CD'],['GH','EF'],['IJ','KL'],['']],[['CD','AB'],['EF','GH'],['KL','IJ'],['']],[['EF','GH'],['CD','AB'],['IJ','KL'],['']],...]

the number of lists are 1500 and each list of them has 16 sublists(the above is just an example).列表数量为 1500 个,每个列表有 16 个子列表(以上仅为示例)。 What I want to do is compare the sublist of the one list with the sublist of the other list , for all lists.对于所有列表,我想要做的是将一个列表的子列表与另一个列表的子列表进行比较 To be more precise, I want to compare ['AB','CD'] with ['CD','AB'] and ['EF','GH'] and the first sublist of the rest of the lists .更准确地说,我想将 ['AB','CD'] 与 ['CD','AB'] 和 ['EF','GH'] 以及列表的 rest 的第一个子列表进行比较 Same follows for the second sublist.第二个子列表也是如此。 I want to check if they have the same items, the order does not matter.我想检查他们是否有相同的项目,顺序无关紧要。 So the ['AB','CD'] with ['CD','AB'] would be same.所以 ['AB','CD'] 和 ['CD','AB'] 是一样的。 But then I need also to find which sublists contain the ['EF','GH'] and do some calculations when all same sublists are together.但是我还需要找到哪些子列表包含 ['EF','GH'] 并在所有相同的子列表在一起时进行一些计算。 All in all, I want to check the 1st sublist of one list with the rest of the first sublists of each list and then do the same for each sublist(2nd,3rd,4th,..,15th).总而言之,我想用每个列表的第一个子列表的 rest 检查一个列表的第一个子列表,然后对每个子列表(第 2、3、4、..、15)执行相同的操作。 I thought of doing something like this我想过做这样的事情

for i in range(0,len(mylist)):
        for j in range(i,len(mylist)):
            if mylist[i][j]==mylist[i + 1][j]:

but it doesn't seem to work when I do so.但是当我这样做时它似乎不起作用。 It gives me:它给了我:

IndexError: list index out of range IndexError:列表索引超出范围

I do not know if my approach is right.我不知道我的方法是否正确。 I tried to search and understand but I could not find something relevant.我试图搜索和理解,但找不到相关的东西。

all sublists are of same length.所有子列表的长度相同。

You are tryng to acessa index out of the list range, when i is on the last position of the list.当 i 在列表的最后一个 position 上时,您正在尝试访问列表范围之外的索引。 You do not need to go to the last position, it' enougth go to penultimate position and compare it with the last, like this: You do not need to go to the last position, it' enougth go to penultimate position and compare it with the last, like this:

for i in range(0,len(mylist)-1):
    for j in range(i,len(mylist[i])):
        if mylist[i][j]==mylist[i+1][j]:
            print('Found')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM