Delete row from another row

I have two string variables - string_A and string_B . string_A contains the following:

"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"

and string_B contains:

"NANA" # "PAA" # "THREE" # "ELI"

I want to remove all elements from string_A from string_B . After I type string_B , it should look like "NANA" # "PAA" # "ELI"

Update:

 x = textA.split(' # ') y = textB.split(' # ') for i, j in enumerate(x): if j in y[i]: pass 
+4
source share
5 answers
 a = '"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"' b = '"NANA" # "PAA" # "THREE" # "ELI"' a_elements = set(a.split(' # ')) b_elements = [key for key in b.split(' # ') if key not in a_elements] b = ' # '.join(b_elements) 
+7
source

Direct solution:

 a = '"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"' b = '"NANA" # "PAA" # "THREE" # "ELI' a_elements = [key.strip() for key in a.split('#')] b_elements = [key.strip() for key in b.split('#')] filtered_b_elements = [key for key in b_elements if key not in a_elements] new_b = ' # '.join(filtered_b_elements) 
+2
source

there you go:

 ' # '.join([x for x in string_B.split(' # ') if x not in string_A.split(' # ')]) 

If you need better performance, first create a list from string_A and use this for the <<21> clause.

+1
source

For re fans:

 import re e = '"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"' f = '"NANA" # "PAA" # "THREE" # "ELI"' ea_pattern = re.compile(r'"([a-zA-Z]+)"') ea = re.findall(ea_pattern,e) fa = re.findall(ea_pattern,f) answer = [x for x in fa if x not in ea] print(answer) 

Use this list any way -

['NANA', 'PAA', 'ELI']

+1
source
 a = '"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"' b = '"NANA" # "PAA" # "THREE" # "ELI"' x = a.split(' # ') y = b.split(' # ') for j in x: if j in y: #HOW TO REMOVE y.remove(j) xx = ' # '.join(x) yy = ' # '.join(y) print xx print yy 

The following outputs:

 '"ONE" # "TWO" # "THREE" # "FOUR" # "FIVE"' '"NANA" # "PAA" # "ELI"' 
0
source

Source: https://habr.com/ru/post/1497221/


All Articles