【问题标题】:Get only unique elements from two lists仅从两个列表中获取唯一元素
【发布时间】:2015-02-11 00:36:19
【问题描述】:

如果我有两个列表(可能有不同的 len):

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

result = [11,22,33,44]

我在做:

for element in f:
    if element in x:
        f.remove(element)

我来了

result = [11,22,33,44,4]

【问题讨论】:

  • 为什么2 不是您输出的一部分? FWIW,你正在做的是f - x,而你想要的是(f \cup x) - (f \cap x)

标签: python list unique


【解决方案1】:

更新:

感谢@Ahito

In : list(set(x).symmetric_difference(set(f)))

Out: [33, 2, 22, 11, 44]

This article 有一个简洁的图表来解释对称差异的作用。

旧答案:

在片场使用Python's documentation

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

我想出了这段代码来从两个列表中获取唯一元素:

(set(x) | set(f)) - (set(x) & set(f))

或稍作修改返回list:

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list

这里:

  1. | 运算符返回 xfboth 中的元素
  2. & 运算符返回 both xf 中的元素
  3. - 运算符从 | 中减去 & 的结果,并为我们提供仅在其中一个列表中唯一呈现的元素

【讨论】:

  • 是否应该将其标记为已接受的答案?有什么不可预见的意外吗?
  • 这段代码通过了严格的压力测试。应该是gtg。
  • Iopheam - 谢谢
  • 拼写为“y”,而不是“i”。 list(set(x).symmetric_difference(set(f)))
  • 已修复。谢谢!
【解决方案2】:

如果您想要两个列表中的唯一元素,这应该可以:

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

res = list(set(x+f))
print(res)
# res = [1, 2, 3, 4, 33, 11, 44, 22]

【讨论】:

  • 但这根本不能回答OP的问题!
  • @lifebalance 你怎么说?!!
  • @JeruLuke 因为问题表明他们需要这个答案[11,22,33,44,4],而不是[1, 2, 3, 4, 33, 11, 44, 22]
  • @YegorKryukov 感谢您纠正我。我没注意这个问题。
【解决方案3】:

基于new (closed) question 中对该问题的澄清:

如果您想要第二个列表中没有出现在第一个列表中的所有项目,您可以编写:

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

result = set(f) - set(x) # correct elements, but not yet in sorted order
print(sorted(result)) # sort and print

# Output: [11, 22, 33, 44]

【讨论】:

    【解决方案4】:
    x = [1, 2, 3, 4]
    
    f = [1, 11, 22, 33, 44, 3, 4]
    
    list(set(x) ^ set(f))
    
    [33, 2, 22, 11, 44]
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
    • ^ 有什么作用?
    【解决方案5】:

    如果您只想从两个列表中获取唯一元素,那么您可以通过..获取它。

    a=[1,2,3,4,5]
    b= [2,4,1]
    list(set(a) - set(b))
    OP:- [3, 5]
    

    【讨论】:

      【解决方案6】:

      输入:

      x = [1,2,3,4]
      
      f = [1,11,22,33,44,3,4]
      

      代码:

      l = list(set(x).symmetric_difference(set(f)))
      print(l)
      

      输出:

      [2, 22, 33, 11, 44]
      

      【讨论】:

        【解决方案7】:

        您的方法不会获得唯一元素“2”。怎么样:

        list(set(x).intersection(f))
        

        【讨论】:

        • 几乎是对的,但你把它弄反了。改用 symmetric_difference()。
        • “两个列表中的唯一元素”对我来说听起来像是交集。提问者应该澄清。
        【解决方案8】:
        v_child_value = [{'a':1}, {'b':2}, {'v':22}, {'bb':23}]
        shop_by_cat_sub_cats = [{'a':1}, {'b':2}, {'bbb':222}, {'bb':23}]
        
        
        unique_sub_cats = []
        for ind in shop_by_cat_sub_cats:
            if ind not in v_child_value:
                unique_sub_cats.append(ind)
        

        unique_sub_cats = [{'bbb': 222}]

        【讨论】:

          【解决方案9】:

          从两个列表创建唯一列表的 Python 代码:

          a=[1,1,2,3,5,1,8,13,6,21,34,55,89,1,2,3]
          b=[1,2,3,4,5,6,7,8,9,10,11,12,2,3,4]
          m=list(dict.fromkeys([a[i] for i in range(0,len(a)) if a [i] in a and a[i] in b and a[i]]))
          
          print(m)
          

          【讨论】:

          • 请解释您的答案,因为不允许仅使用代码的答案。谢谢!
          【解决方案10】:
          L=[] 
          For i in x:
              If i not in f:
                  L. Append(I)
          For i in f:
              If I not in x:
                  L. Append(I)
          Return L
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-01-17
            • 2020-04-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-23
            • 2012-12-16
            相关资源
            最近更新 更多