Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding matching strings when comparing two lists

I am trying to compare two lists to see if there are any matching strings within the lists.

lets say list_1 is

['GAAGGTCGAA', 'GAAGGTCGA', 'AAGGTCGAA', 'GAAGGTCG', 'AAGGTCGA', 'AGGTCGAA', 'GAAGGTC', 'AAGGTCG', 'AGGTCGA', 'GGTCGAA', 'GAAGGT', 'AAGGTC', 'AGGTCG', 'GGTCGA', 'GTCGAA', 'GAAGG', 'AAGGT', 'AGGTC', 'GGTCG', 'GTCGA', 'TCGAA', 'GAAG', 'AAGG', 'AGGT', 'GGTC', 'GTCG', 'TCGA', 'CGAA', 'GAA', 'AAG', 'AGG', 'GGT', 'GTC', 'TCG', 'CGA', 'GAA', 'GA', 'AA', 'AG', 'GG', 'GT', 'TC', 'CG', 'GA', 'AA', 'G', 'A', 'A', 'G', 'G', 'T', 'C', 'G', 'A', 'A']

and list_2 is

['CCTCGGGA', 'CCTCGGG', 'CTCGGGA', 'CCTCGG', 'CTCGGG', 'TCGGGA', 'CCTCG', 'CTCGG', 'TCGGG', 'CGGGA', 'CCTC', 'CTCG', 'TCGG', 'CGGG', 'GGGA', 'CCT', 'CTC', 'TCG', 'CGG', 'GGG', 'GGA', 'CC', 'CT', 'TC', 'CG', 'GG', 'GG', 'GA', 'C', 'C', 'T', 'C', 'G', 'G', 'G', 'A', '', '', '', '', '', '', '', '', '', 'CCTCGGG', '', '', '', '', '', '', '', '', '']

I know that "TCG" is in both lists, but what type of function can I use to determine this within python? I tried a for loop and many other built in functions but I can't get it to work.

like image 824
Rsherrill Avatar asked Oct 14 '15 00:10

Rsherrill


2 Answers

This is actually easy with set

print set(list_1) & set(list_2)
print bool(set(list_1) & set(list_2))
# set(['A', 'C', 'G', 'CG', 'GG', 'T', 'GA', 'TCG', 'TC']) //common strings
# true //Boolean result

DEMO

http://ideone.com/y0GltE

like image 165
Pedro Lobito Avatar answered Oct 14 '22 22:10

Pedro Lobito


bool(set(list_1) & set(list_2))

like image 21
Alex Hall Avatar answered Oct 14 '22 22:10

Alex Hall