<tfoot id='X5Wnk'></tfoot>

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

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

    1. 查找列表中不常见的元素

      时间:2023-07-03
          <tbody id='dMtHV'></tbody>

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

        2. <small id='dMtHV'></small><noframes id='dMtHV'>

        3. <tfoot id='dMtHV'></tfoot>

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

              • 本文介绍了查找列表中不常见的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试编写一段可以自动分解表达式的代码.例如,如果我有两个列表 [1,2,3,4] 和 [2,3,5],代码应该能够找到两个列表 [2,3] 中的共同元素,并结合其余的元素一起在一个新列表中,即 [1,4,5].

                I'm trying to write a piece of code that can automatically factor an expression. For example, if I have two lists [1,2,3,4] and [2,3,5], the code should be able to find the common elements in the two lists, [2,3], and combine the rest of the elements together in a new list, being [1,4,5].

                来自这篇文章:如何找到列表交集?我看到共同的元素可以通过

                From this post: How to find list intersection? I see that the common elements can be found by

                set([1,2,3,4]&set([2,3,5]). 
                

                有没有一种简单的方法可以从每个列表中检索非常见元素,在我的示例中是 [1,4] 和 [5]?

                Is there an easy way to retrieve non-common elements from each list, in my example being [1,4] and [5]?

                我可以继续做一个 for 循环:

                I can go ahead and do a for loop:

                lists = [[1,2,3,4],[2,3,5]]
                conCommon = []
                common = [2,3]
                for elem in lists:
                    for elem in eachList:
                    if elem not in common:
                        nonCommon += elem
                

                但这似乎是多余且低效的.Python 是否提供任何方便的函数来做到这一点?提前致谢!!

                But this seems redundant and inefficient. Does Python provide any handy function that can do that? Thanks in advance!!

                推荐答案

                set 使用对称差分运算符(也称为 XOR 运算符):

                Use the symmetric difference operator for sets (aka the XOR operator):

                >>> set([1,2,3]) ^ set([3,4,5])
                set([1, 2, 4, 5])
                

                这篇关于查找列表中不常见的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python设置1和True的interpetation 下一篇:Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?

                相关文章

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

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