繁体   English   中英

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

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

我有两个列表,我想从列表中删除相同的单词。 我使用此代码,但它不起作用:

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

我想要的 output 是a = ['watch TV', 'sit ting', 'note book'] 有人可以帮我吗?

首先你的答案对我来说似乎不清楚,但我想你想这样做

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'}

您可以使用Set执行差异、交集、并集、对称差异等操作。

暂无
暂无

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

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