• <bdo id='j9j6u'></bdo><ul id='j9j6u'></ul>

        <legend id='j9j6u'><style id='j9j6u'><dir id='j9j6u'><q id='j9j6u'></q></dir></style></legend>
        <tfoot id='j9j6u'></tfoot>

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

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

        有没有办法在 Python 中获取元组或列表的差异和交集?

        时间:2023-08-31
        • <legend id='vjz0r'><style id='vjz0r'><dir id='vjz0r'><q id='vjz0r'></q></dir></style></legend>
            <tbody id='vjz0r'></tbody>

          1. <tfoot id='vjz0r'></tfoot>

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

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

                  本文介绍了有没有办法在 Python 中获取元组或列表的差异和交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有清单:

                  a = [1, 2, 3, 4, 5]
                  b = [4, 5, 6, 7, 8]
                  
                  c = a * b
                  

                  应该给我:

                  c = [4, 5]
                  

                  c = a - b
                  

                  应该给我:

                  c = [1, 2, 3]
                  

                  这适用于 Python 还是我必须自己编写?

                  Is this available for Python or do I have to write it myself?

                  元组也可以这样吗?我可能会使用列表,因为我将添加它们,但只是想知道.

                  Would the same work for tuples? I will likely use lists as I will be adding them, but just wondering.

                  推荐答案

                  如果顺序无所谓,可以使用set 为此.它实现了交集和差异.

                  If the order doesn't matter, you can use set for this. It has intersection and difference implemented.

                  >>> a = set([1, 2, 3, 4, 5])
                  >>> b = set([4, 5, 6, 7, 8])
                  >>> a.intersection(b)
                  set([4, 5])
                  >>> a.difference(b)
                  set([1, 2, 3])
                  

                  以下是这些操作的时间复杂度信息:https://wiki.python.org/moin/TimeComplexity#set.请注意,减数的顺序会改变运算复杂度.

                  Here is the info of time complexities of these operations: https://wiki.python.org/moin/TimeComplexity#set. Notice, that the order of subtrahends changes operation complexity.

                  如果元素可以出现多次(正式名称为 multiset),您可以使用 Counter:

                  If element can occur several times (formally it is called multiset), you can use Counter:

                  >>> from collections import Counter
                  >>> a = Counter([1, 2, 3, 4, 4, 5, 5])
                  >>> b = Counter([4, 4, 5, 6, 7, 8])
                  >>> a - b
                  Counter({1: 1, 2: 1, 3: 1, 5: 1})
                  >>> a & b
                  Counter({4: 2, 5: 1})
                  

                  这篇关于有没有办法在 Python 中获取元组或列表的差异和交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python:生成“元组集";来自“元组列表";不考虑顺序 下一篇:在元组列表中按字母对数字求和

                  相关文章

                        <bdo id='OBYfm'></bdo><ul id='OBYfm'></ul>
                    1. <small id='OBYfm'></small><noframes id='OBYfm'>

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