简体   繁体   English

如何从两个列表中删除相同的单词?

[英]How to remove the same words from two lists?

I have two lists that I want to remove the same words from the lists.我有两个列表,我想从列表中删除相同的单词。 I use this code but it doesn't work:我使用此代码,但它不起作用:

a = ['go ing', 'watch TV', 'ice cream', 'sit ting', 'note book']
b = ['go ing', 'watching TV', 'ice cream', 'sit ing', 'notebook']

if a[i] == b[i]:
    try:
        a.remove(i)
        b.remove(i)
    except:
        pass

My desired output is a = ['watch TV', 'sit ting', 'note book'] .我想要的 output 是a = ['watch TV', 'sit ting', 'note book'] Could anyone help me?有人可以帮我吗?

First of all your answer seems unclear to me, but I guess you want to do this首先你的答案对我来说似乎不清楚,但我想你想这样做

a = ['go ing','watch TV', 'ice cream','sit ting','note book']
b = ['go ing','watching TV','ice cream','sit ing','notebook']

set(a).intersection(set(b))
>>> {'go ing', 'ice cream'}

set(a).difference(set(b))
>>> {'sit ting', 'watch TV', 'note book'}

You can use Set to perform operations like Difference, Intersection, Union, Symmetric Difference, etc.您可以使用Set执行差异、交集、并集、对称差异等操作。

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

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