0

i have two list and i want to delete the matching one and keep the different. here is the code:

def check_synonym(text):
     tokens=regexp_tokenize(text, r'[،؟!.؛\s+]\s*', gaps=True)
     based_text= ' '.join(cursor.execute('SELECT request FROM Male_Conversation_Engine WHERE request REGEXP?',[tokens[0]]).fetchone())
     based_tokens=regexp_tokenize(str(based_text), r'[،؟!.؛\s+]\s*', gaps=True)
    for w1 in based_tokens:
       for w2 in tokens:
           if w1 == w2:
              based_tokens.remove(w1),tokens.remove(w2)      
return list

if the two list are "in Arabic":

 tokens = ['هذا','الجهاز','الجميل']
 based_tokens = ['هذا','الجهاز','جيد']

the Output should be:

 tokens = ['الجميل']
 based_tokens = ['جيد']

the actual Output:

 tokens = ['الجهاز','جميل']
 based_tokens = ['الجهاز','جيد']

the side only delete the first element 'هذا' and return the rest of the list.

(using python3)

Eman S.
  • 145
  • 1
  • 8
  • Possible duplicate of [Python, compute list difference](http://stackoverflow.com/questions/6486450/python-compute-list-difference) – Ayush Oct 18 '16 at 17:24

2 Answers2

0

You can use a combination of sets and list comprehensions

s1 = set(tokens)
s2 = set(based_tokens)

tokens = [t for t in tokens if t not in s2]
based_tokes = [t for t in based_tokens if t not in s1]

The only reason I am using sets is because for large lists it is much faster to check membership with sets.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0
    set1=set(tokens)
    set2=set(based_tokens)

    tokens = set1-set2 
    based_tokens = set2-set1
Bharat Gaikwad
  • 530
  • 2
  • 5
  • 16