简体   繁体   English

从两个列表中删除重复项

[英]Removing duplicates from two lists

I have written some code in python to remove duplicate items from lists. 我在python中编写了一些代码,以从列表中删除重复项。 I have two separate lists that look like: 我有两个单独的列表,如下所示:

lhsNet = ['p', 'p', 'p', 'p', '(2)H', 'p', '(2)H', 'p', '(3)He', '(3)He']

rhsNet = ['(2)H', 'e+', 'nu_e', '(2)H', 'e+', 'nu_e', '(3)He', 'gamma', '(3)He', 'gamma', '(4)He', 'p', 'p']

Here is the code: 这是代码:

for x in lhsNet:
    for z in rhsNet:
        if x == z:
            lhsNet.remove(x)
            rhsNet.remove(z)
            break

The code should find a duplicate entry that exists in both lists and remove it. 该代码应找到两个列表中都存在的重复条目并将其删除。 For some reason after execution I am left with: 执行后由于某种原因,我留下了:

lhsNet = ['p', 'p', 'p', 'p', '(3)He']

rhsNet = ['e+', 'nu_e', 'e+', 'nu_e', 'gamma', '(3)He', 'gamma', '(4)He']

Clearly it has removed all of the duplicate entries that exist in both lists EXCEPT for the last '(3)He' . 显然,它删除了两个列表中都存在的所有重复项,但最后一个'(3)He' Can anyone explain to me what is going wrong in my code and how to fix it? 谁能向我解释我的代码出了什么问题以及如何解决?

I think that should work out for you: 我认为应该为您解决:

new_lhsNet = list(set(lhsNet) - set(rhs_net))
new_rhsNet = list(set(rhsNet) - set(lhsNet))

Edit: Otherwise you can try the following as well: 编辑:否则,您也可以尝试以下操作:

lhsNet = [x for x in lhsNet if not x in rhsNet]
rhsNet = [x for x in rhsNet if not x in lhsNet]

The set() method from the upper example removes every duplicate within the list itself, which is probably not what you want. 上例中的set()方法删除列表本身内的所有重复项,这可能不是您想要的。 The lower example removes only the duplicates from another list without the duplicates within the list itself. 下面的示例仅从另一个列表中删除重复项,而不在列表本身内删除重复项。

You shouldn't use break statement when you iterate If you just remove it and make your code look like. 如果只是删除它并使代码看起来像,则在迭代时不应使用break语句。

    for x in lhsNet:
        for z in rhsNet:
            if x == z:
               lhsNet.remove(x)
               rhsNet.remove(z)

Everything will be working perfectly 一切都会完美地进行

删除break语句,它将根据需要运行。

Once the data is matched with rhsNet,the inner loop will break,it will not iterate through entire data of rhsNet list. 数据与rhsNet匹配后,内部循环将中断,不会遍历rhsNet列表的整个数据。 So, Please remove the break statement in the code and able to get the unique values in both list. 因此,请删除代码中的break语句,并能够在两个列表中获得唯一值。

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

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