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

    <tfoot id='xbrlP'></tfoot>

      <bdo id='xbrlP'></bdo><ul id='xbrlP'></ul>

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

    <legend id='xbrlP'><style id='xbrlP'><dir id='xbrlP'><q id='xbrlP'></q></dir></style></legend>

    1. 如何比较python中的列表/集合列表?

      时间:2023-07-03
      • <i id='KaZkI'><tr id='KaZkI'><dt id='KaZkI'><q id='KaZkI'><span id='KaZkI'><b id='KaZkI'><form id='KaZkI'><ins id='KaZkI'></ins><ul id='KaZkI'></ul><sub id='KaZkI'></sub></form><legend id='KaZkI'></legend><bdo id='KaZkI'><pre id='KaZkI'><center id='KaZkI'></center></pre></bdo></b><th id='KaZkI'></th></span></q></dt></tr></i><div id='KaZkI'><tfoot id='KaZkI'></tfoot><dl id='KaZkI'><fieldset id='KaZkI'></fieldset></dl></div>
            <bdo id='KaZkI'></bdo><ul id='KaZkI'></ul>
              <tbody id='KaZkI'></tbody>

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

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

                本文介绍了如何比较python中的列表/集合列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                比较两个列表/集合并输出差异的最简单方法是什么?是否有任何内置函数可以帮助我比较嵌套列表/集合?

                What is the easiest way to compare the 2 lists/sets and output the differences? Are there any built in functions that will help me compare nested lists/sets?

                输入:

                First_list = [['Test.doc', '1a1a1a', 1111], 
                              ['Test2.doc', '2b2b2b', 2222],  
                              ['Test3.doc', '3c3c3c', 3333]
                             ]  
                Secnd_list = [['Test.doc', '1a1a1a', 1111], 
                              ['Test2.doc', '2b2b2b', 2222], 
                              ['Test3.doc', '8p8p8p', 9999], 
                              ['Test4.doc', '4d4d4d', 4444]]  
                

                预期输出:

                Differences = [['Test3.doc', '3c3c3c', 3333],
                               ['Test3.doc', '8p8p8p', 9999], 
                               ['Test4.doc', '4d4d4d', 4444]]
                

                推荐答案

                所以你想要两个项目列表之间的差异.

                So you want the difference between two lists of items.

                first_list = [['Test.doc', '1a1a1a', 1111], 
                              ['Test2.doc', '2b2b2b', 2222], 
                              ['Test3.doc', '3c3c3c', 3333]]
                secnd_list = [['Test.doc', '1a1a1a', 1111], 
                              ['Test2.doc', '2b2b2b', 2222], 
                              ['Test3.doc', '8p8p8p', 9999], 
                              ['Test4.doc', '4d4d4d', 4444]]
                

                首先,我将每个列表列表转换为元组列表,因为元组是可散列的(列表不是),因此您可以将元组列表转换为一组元组:

                First I'd turn each list of lists into a list of tuples, so as tuples are hashable (lists are not) so you can convert your list of tuples into a set of tuples:

                first_tuple_list = [tuple(lst) for lst in first_list]
                secnd_tuple_list = [tuple(lst) for lst in secnd_list]
                

                然后你就可以做套路了:

                Then you can make sets:

                first_set = set(first_tuple_list)
                secnd_set = set(secnd_tuple_list)
                

                编辑(由 sdolan 建议):您可以为单行中的每个列表完成最后两个步骤:

                EDIT (suggested by sdolan): You could have done the last two steps for each list in a one-liner:

                first_set = set(map(tuple, first_list))
                secnd_set = set(map(tuple, secnd_list))
                

                注意:map 是一个函数式编程命令,它将第一个参数中的函数(在本例中为 tuple 函数)应用于第二个参数中的每个项目(即在我们的例子中是一个列表的列表).

                Note: map is a functional programming command that applies the function in the first argument (in this case the tuple function) to each item in the second argument (which in our case is a list of lists).

                并找出集合之间的对称差:

                and find the symmetric difference between the sets:

                >>> first_set.symmetric_difference(secnd_set) 
                set([('Test3.doc', '3c3c3c', 3333),
                     ('Test3.doc', '8p8p8p', 9999),
                     ('Test4.doc', '4d4d4d', 4444)])
                

                注意 first_set ^ secnd_set 等价于 symmetric_difference.

                此外,如果您不想使用集合(例如,使用 python 2.2),它也很简单.例如,使用列表推导:

                Also if you don't want to use sets (e.g., using python 2.2), its quite straightforward to do. E.g., with list comprehensions:

                >>> [x for x in first_list if x not in secnd_list] + [x for x in secnd_list if x not in first_list]
                [['Test3.doc', '3c3c3c', 3333],
                 ['Test3.doc', '8p8p8p', 9999],
                 ['Test4.doc', '4d4d4d', 4444]]
                

                或使用功能性 filter 命令和 lambda 函数.(你必须测试两种方式并结合起来).

                or with the functional filter command and lambda functions. (You have to test both ways and combine).

                >>> filter(lambda x: x not in secnd_list, first_list) + filter(lambda x: x not in first_list, secnd_list)
                
                [['Test3.doc', '3c3c3c', 3333],
                 ['Test3.doc', '8p8p8p', 9999],
                 ['Test4.doc', '4d4d4d', 4444]]
                

                这篇关于如何比较python中的列表/集合列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是python的“集合"吗?稳定的? 下一篇:如何制作一组列表

                相关文章

              1. <small id='nVbjf'></small><noframes id='nVbjf'>

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