Merge 2 lists and delete double entries

I have a piece of code that loads 2 lists using this code:

with open('blacklists.bls', 'r') as f: L = [dnsbls.strip() for dnsbls in f] with open('ignore.bls', 'r') as f2: L2 = [ignbls.stip() for ignbls in f2] 

dnsbls contains:

 list1 list2 list3 

ignbls contains

 list2 

What I want to do is combine dnsbls and ignbls and then delete any lines that appear more than once and print them with "for". I thought something like:

 for combinedlist in L3: print combinedlist 

What in the example aboe will print:

 list1 list3 
+4
source share
2 answers

You need to use sets instead of lists:

 L3 = list(set(L).difference(L2)) 

Demonstration:

 >>> L=['list1','list2','list3'] >>> L2=['list2'] >>> set(L).difference(L2) set(['list1', 'list3']) >>> list(set(L).difference(L2)) ['list1', 'list3'] 

For your purposes, you probably do not need to convert it to a list again, you can iterate over the resulting result set simply.

+2
source

If you ignore fewer blacklists (as a rule, I think), then (untested):

 with open('blacklists.bls') as bl, open('ignore.bls') as ig: bl_for = (line.strip() for line in bl if 'for' not in line) ig_for = (line.strip() for line in ig if 'for' not in line) res = set(ig_for).difference(bl_for) 
+1
source

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


All Articles