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']
. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.